标签:
Time Limit: 1000MS | Memory Limit: 20000K | |
Total Submissions: 34353 | Accepted: 16669 |
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
Source
1 /*by SilverN*/ 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 using namespace std; 8 const int mxn=50010; 9 int n,m,ans; 10 int fa[mxn]; 11 int find(int x){ 12 if(fa[x]==x)return x; 13 return fa[x]=find(fa[x]); 14 } 15 int main(){ 16 int i,j; 17 while(scanf("%d%d",&n,&m) && (n|m)){ 18 for(i=0;i<n;i++)fa[i]=i; 19 ans=0; 20 int x,y; 21 for(i=1;i<=m;i++){ 22 int k; 23 scanf("%d",&k); 24 for(j=1;j<=k;j++){ 25 scanf("%d",&x); 26 if(j!=1){ 27 x=find(x); 28 y=find(y); 29 if(x!=y)fa[x]=y; 30 } 31 y=x; 32 } 33 } 34 for(i=0;i<n;i++) 35 if(find(i)==find(0))ans++; 36 printf("%d\n",ans); 37 } 38 return 0; 39 }
标签:
原文地址:http://www.cnblogs.com/SilverNebula/p/5898461.html