首页 > 代码库 > POJ 1611 The Suspects
POJ 1611 The Suspects
The Suspects
Time Limit: 1000MS | Memory Limit: 20000K | |
Total Submissions: 21525 | Accepted: 10426 |
Description
Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others.
In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP).
Once a member in a group is a suspect, all members in the group are suspects.
However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.
In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP).
Once a member in a group is a suspect, all members in the group are suspects.
However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.
Input
The input file contains several cases. Each test case begins with two integers n and m in a line, where n is the number of students, and m is the number of groups. You may assume that 0 < n <= 30000 and 0 <= m <= 500. Every student is numbered by a unique integer between 0 and n−1, and initially student 0 is recognized as a suspect in all the cases. This line is followed by m member lists of the groups, one line per group. Each line begins with an integer k by itself representing the number of members in the group. Following the number of members, there are k integers representing the students in this group. All the integers in a line are separated by at least one space.
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.
Output
For each case, output the number of suspects in one line.
Sample Input
100 42 1 25 10 13 11 12 142 0 12 99 2200 21 55 1 2 3 4 51 00 0
Sample Output
411
1 #include<stdio.h> 2 #include<iostream> 3 #include<string.h> 4 #define MAXN 30030 5 using namespace std; 6 int Rank[MAXN],father[MAXN]; 7 int find(int x) 8 { 9 if(x!=father[x])10 father[x]=find(father[x]);11 return father[x];12 }13 void Union (int x,int y)14 {15 int t1,t2;16 t1=find(x);17 t2=find(y);18 if(t1==t2)19 return ;20 if(Rank[t1]>Rank[t2])21 father[t2]=t1;22 else23 {24 father[t1]=t2;25 if(Rank[t1]==Rank[t2])26 Rank[t2]++;27 }28 }29 int main()30 {31 int n,m,i,j,k,x,t;32 while(cin >> n >> m)33 {34 if(n==0&&m==0) break;35 for(i=0; i<=n; i++)36 {37 Rank[i]=0;38 father[i]=i;39 }40 for(i=1; i<=m; i++)41 {42 cin >> k;43 if(k>=1)44 cin >> x;45 for(j=1; j<k; j++)46 {47 cin >> t;48 Union(x,t);49 }50 }51 int ans=1,cmp=find(0);52 for(i=1; i<n; i++)53 if(find(i)==cmp)54 ans++;55 cout << ans << endl;56 }57 return 0;58 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。