hihocoder 1343 : Stable Members(拓扑排序)

题目链接:http://hihocoder.com/problemset/problem/1343?sid=1318197


时间限制: 10000ms
单点时限: 1000ms
内存限制: 256MB

描述

Recently Little Hi joined an algorithm learning group. The group consists of one algorithm master and Nmembers. The members are numbered from 1 to N. Each member has one or more other members as his mentors. Some members' mentor is the master himself.

Every week each member sends a report of his own learning progress and the reports collected from his pupils (if there is any) to his mentors. The group is so well designed that there is no loop in the reporting chain so no one receives his own report from his pupil. And finally the master gets every one's report (maybe more than once).

Little Hi notices that for some members their reporting routes to the master can be easily cut off by a single member's (other than the master and himself) absence from the reporting duty. They are called unstable members while the others are stable members. Given the reporting network of the group, can you find out how many members are stable?

Assume there are 4 members in the group. Member 1 and 2 both have the master as their only mentor. Member 3 has 2 mentors: member 1 and member 2. Member 4 has 1 mentor: member 3. Then member 4 is the only unstable member in the group because if member 3 is absent his learning report will be unable to be sent to the master. 

输入

The first line contains an integer N, the number of members.

The i-th line of the following N lines describe the mentors of the i-th member. The first integer is Ki, the number of mentors of the i-th member. Then follows Ki integers A1 ... AN, which are his mentors' numbers. Number 0 indicates that the master is one of his mentor.

For 40% of the data, 1 ≤ N ≤ 1000.

For 100% of the data, 1 ≤ N ≤ 100000.

For 100% of the data, 1 ≤ Ki ≤ 10, Ki < N, 0 ≤ Ai ≤ N

输出

Output the number of stable members.

样例输入
5
1 0
1 0
2 1 2
1 3
2 4 3 
样例输出
3


题目大意:

一个有向无环图,最上游的点只有一个。若删掉途中某一个点,则某些后续点无法与最上游的点连通,则这些后续点为不稳定的。要找出所有不稳定的点,然后输出剩下的稳定点的数量。

题目思路:

拓扑排序。

对于每个顶点v,采用染色的方法:即对于某个顶点v,采用拓扑排序的方法遍历其后续顶点,并依次染色。后续的顶点进入队列的条件是,其所有的父顶点都已经被染成顶点v的颜色。

例如样例:因为会出现这样的情况,当删掉3时,虽然5的入边有2条,但也是unstable的。因为4的颜色也是染成了3的颜色,所以对于5来说其实还是只有一个节点才能到他。


代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<map>

using namespace std;

#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)

const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const double DINF = 0xffffffffffff;
const int mod = 1e9+7;
const int N = 1e5+5;

int n;
vector<int>son[N];
vector<int>parent[N];
int unstable[N];  //记录i点是否不稳定
int color[N];     //染色判定数组,初始值全部为0

bool all_colored(int now,int col)   //判断某个点其所有父节点是否被染成了一个颜色
{
    int len=parent[now].size();
    for(int i=0;i<len;i++)
        if(color[parent[now][i]]!=col)
            return false;
    return true;
}

void topSort(int s)
{
    if(color[s]!=0)
        return ;
    color[s]=s;
    queue<int>q;
    while(!q.empty())
        q.pop();
    q.push(s);
    while(!q.empty())
    {
        int now = q.front();
        q.pop();
        int len = son[now].size();
        for(int i=0;i<len;i++)
        {
            if(all_colored(son[now][i],color[now]))
            {
                color[son[now][i]]=color[now];   //把子节点颜色染成父节点
                q.push(son[now][i]);             //子节点入队
                unstable[son[now][i]]=1;         //子节点为不稳定点
            }
        }
    }
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    std::ios::sync_with_stdio(false);
    int t,x;
    while(cin>>n)
    {
        for(int i=1;i<=n;i++)
        {
            cin>>t;
            while(t--)
            {
                cin>>x;
                son[x].push_back(i);     //注意谁是父,谁是子
                parent[i].push_back(x);
            }
        }
        MEM(unstable,0);
        MEM(color,0);
        for(int i=1;i<=n;i++)
            topSort(i);
        int ans=0;
        for(int i=1;i<=n;i++)
            ans+=unstable[i];
        cout<<n-ans<<endl;
    }
    return 0;
}


更多方法可参考:http://www.cnblogs.com/demian/p/6536799.html

猜你喜欢

转载自blog.csdn.net/baodream/article/details/80372700
今日推荐