hihoCoder1343 : Stable Members【BFS拓扑排序】

题目链接:https://hihocoder.com/problemset/problem/1343

#1343 : Stable Members

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

描述

Recently Little Hi joined an algorithm learning group. The group consists of one algorithm master and N members. 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 ≤ AiN.

输出

Output the number of stable members.

样例输入
5
1 0
1 0
2 1 2
1 3
2 4 3 
样例输出
3
题意:给一个有向无环图,定义一个点为unstable当且仅当删掉一个点(不能为它自己或点0)时,它不能与点0连通;其他点则为stable,求图中有几个stable点。
样例解释:
如左图所示,删掉点3,则4和5都无法连通到点0即master,所以4和5都是unstable点,1、2和3都是stable,所以最后答案为3个stable点。
题解:依次对点v进行拓扑排序,遍历其后续点,如果后续点的所有父节点都染色为v,则也染色为v,并入队列,标记为unstable点。
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N = 100001;
 4 struct node {
 5     int color = 0;
 6     vector<int>s, p;//子节点、父节点
 7 }a[N];
 8 bool unstable[N];
 9 bool all_colored(int v, int color) {
10     int num = a[v].p.size();
11     bool flag = true;
12     for(int i = 0; flag && i < num; ++i)
13         flag &= (a[a[v].p[i]].color == color);
14     return flag;
15 }
16 void topo(int v) {
17     if(unstable[v]) return;
18     queue<int>q;
19     q.push(v);
20     a[v].color = v;
21     while(!q.empty()) {
22         int u = q.front(); q.pop();
23         int num = a[u].s.size();
24         for(int i = 0; i < num; ++i) {
25             int son = a[u].s[i];
26             if(all_colored(son, v)) {
27                 a[son].color = v;
28                 unstable[son] = true;
29                 q.push(son);
30             }
31         }
32     }
33 }
34 int main() {
35     int n, k, i, v, ans = 0;
36     scanf("%d", &n);
37     for(i = 1; i <= n; ++i) {
38         scanf("%d", &k);
39         while(k--) {
40             scanf("%d", &v);
41             a[i].p.push_back(v);
42             a[v].s.push_back(i);
43         }
44     }
45     for(i = 1; i <= n; ++i) topo(i);
46     for(i = 1; i <= n; ++i) ans += unstable[i];
47     printf("%d\n", n - ans);
48     return 0;
49 }
View Code

 

还有其他解法,参考:http://www.cnblogs.com/demian/p/6536799.html
posted @ 2018-04-28 21:59  GraceSkyer  阅读(447)  评论(0编辑  收藏  举报

~~~~~~ACM大牛语录,激励一下~~~~~~

为了世界的和平,为了女生的安全,我拼命做题,做题,做题!

用最短的时间,刷最多的题!

给我一滴泪,我就看到了你全部的海洋!

seize the hour, seize the day.

人生难免有无奈,幸福走远了,或是感叹幸福来迟了.其实我一直相信,无论手中的幸福是多么微不足道的感觉,我会把握住那每一分,每一秒,当幸福依旧像那百鸟般飞逝,终究无法掌握时,我会感谢它,曾经降临过!

A自己的题,让别人郁闷去吧

WA肠中过,AC心中留 TLE耳边过,AC特别牛

天然的悲苦和伤逝,过去有过,以后还会有

^*^一步一步往上爬^*^

AC就像练级,比赛就像PK. 练级不如PK好玩

其实,世上本没有ACM,AC的人多了,也便有了!

AC无止尽~ Seek you forever~

找呀找呀找水题,找到一个AC一个呀!

AC是检验程序的唯一标准。

真的猛士,敢于直面惨淡的人生,敢于正视淋漓的鲜血……