//1480 #include<stdio.h> const int MAX=50010; int p[MAX]; int n,m; void init(int k) { for (int i=1; i<=k; i++) p[i]=i; } int find(int x) //?????? { if (x==p[x]) return x; else return p[x]=find(p[x]); } void merge(int x,int y) //?????? { int px,py; px=find(x); py=find(y); if (px!=py) {p[px]=py;n--;} } int main() { int i,y,x,c=1; while(scanf("%d%d",&n,&m)!=EOF) { init(n); for(i=1;i<=m;i++) { scanf("%d%d",&x,&y); merge(x,y); } printf("Case %d: %d\n",c++,n); } return 0; }
//1480
#include<stdio.h>
const int MAX=50010;
int p[MAX];
int n,m;
void init(int k)
{ for (int i=1; i<=k; i++) p[i]=i; }
int find(int x) //??????
{ if (x==p[x]) return x;
else return p[x]=find(p[x]); }
void merge(int x,int y) //??????
{ int px,py; px=find(x); py=find(y); if (px!=py) {p[px]=py;n--;}
}
int main()
{
int i,y,x,c=1;
while(scanf("%d%d",&n,&m)!=EOF)
{
init(n);
for(i=1;i<=m;i++)
{ scanf("%d%d",&x,&y);
merge(x,y); }
printf("Case %d: %d\n",c++,n);
}
return 0;
}
原文:http://www.cnblogs.com/2014acm/p/3903276.html