标签:
Description
Input
Output
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
1 #include<iostream>
2 using namespace std;
3 int a[30010];
4 int find(int x)
5 {
6 int r=x;
7 while(r!=a[r])
8 r=a[r];
9 int i=x,j;
10 while(a[i]!=r)
11 {
12 j=a[i];
13 a[i]=r;
14 i=j;
15 }
16 return r;
17 }
18 int main()
19 {
20 int n,m,k,i,fx,fy,x,y,res;
21 while(cin>>n>>m)
22 {
23 res=0;
24 if(n==0&&m==0) break;
25 for(i=0;i<n;i++)
26 a[i]=i;
27 while(m--)
28 {
29 cin>>k>>x;
30 k--;
31 while(k--)
32 {
33 fx=find(x);
34 cin>>y;
35 fy=find(y);
36 a[fx]=fy;
37 }
38 }
39 for(i=0;i<n;i++)
40 {
41 if(find(0)==find(i)) res++;
42 }
43 cout<<res<<endl;
44 }
45 system("pause");
46 return 0;
47 }
标签:
原文地址:http://www.cnblogs.com/m1031478472/p/5293658.html