题目:
The Suspects
Time Limit: 1000MS | Memory Limit: 20000K | |
Total Submissions: 35876 | Accepted: 17432 |
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 4 2 1 2 5 10 13 11 12 14 2 0 1 2 99 2 200 2 1 5 5 1 2 3 4 5 1 0 0 0
Sample Output
4 1 1
第一组样例共100人,4个小组 第一个小组2人,1和2....其中0感染,故共 0 1 2 99 四人感染
思路:
①union后找到0的根节点,遍历看有多少个点与0的根节点相同
②也可以对每一个结点加入一个num[i]属性,用来记录该结点所在集合中结点的个数,union时更新num[i],最后输出num[par[0]]
思路二参:http://www.cnblogs.com/ktyanny/archive/2009/12/09/1620304.html
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<ctype.h> //tower()
#include<set>
#include<map>
#include<iomanip> // cout<<setprecision(1)<<fixed<<a;
#include<vector>
#include<time.h>
#include<cmath>
#include<algorithm>
#include<bitset>
#include<limits.h>
#include<stack>
#include<queue>
using namespace std;
const int maxn=30010;
const int inf=0x7fffffff;
int par[maxn],depth[maxn];
int find(int x){
int s,k,j;
s=k=x;
while(s!=par[s]) s=par[s];
while(k!=s){
j=par[k];
par[k]=s;
k=j;
}
return s;
}
void Union(int x,int y){
x=find(x);
y=find(y);
if(x==y) return;
if(depth[x]<depth[y]) par[x]=y;
else{//秩小的接在秩大的下面
par[y]=x;
if(depth[x]==depth[y]) depth[x]++;
}
}
int main(){// 352K 16MS
int n,m,k,a,b,ans;
while(scanf("%d%d",&n,&m)==2){
if((!n)&&(!m)) break;
for(int i=0;i<n;i++){
par[i]=i;
depth[i]=0;
}
while(m--){//读入技巧:先读一个 后面与之合并
scanf("%d",&k);
if(k==0) continue;
scanf("%d",&a);
for(int i=1;i<k;++i){
scanf("%d",&b);
Union(a,b);
}
}
ans=1;
int t=find(0);
for(int i=1;i<n;++i){
if(find(i)==t) ans++;
}
printf("%d\n",ans);
}
return 0;
}