poj 1611 The Suspects

就是简单的并查集应用,直接套模板都不带有改动的

 1 #include<stdio.h>
2 #include<iostream>
3 #include<string.h>
4 using namespace std;
5 #define N 30001
6 int a[N];
7 int find(int x)
8 {
9 if(a[x]!=x) return a[x]=find(a[x]);
10 return a[x];
11 }
12 int main()
13 {
14 int i,k;
15 int n,m;
16 while(cin>>n>>m,n+m)
17 {
18 for(i=0;i<n;i++)
19 {
20 a[i]=i;
21 }
22 int x,y;
23 for(i=0;i<m;i++)
24 {
25 cin>>k;
26 if(k)cin>>x;
27 k--;
28 while(k--)
29 {
30 cin>>y;
31 int root1=find(x);
32 int root2=find(y);
33 if(root1!=root2) a[root2]=root1;
34 x=y;
35 }
36 }
37 int sum=0;
38 for(i=0;i<n;i++)
39 if(find(i)==a[0]) sum++;
40 cout<<sum<<endl;
41 }
42 return 0;
43 }
原文地址:https://www.cnblogs.com/fxh19911107/p/2380880.html