标签:des blog io ar os sp for strong div
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 25123 | Accepted: 12388 |
Description
Input
Output
Sample Input
10 9 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 10 4 2 3 4 5 4 8 5 8 0 0
Sample Output
Case 1: 1 Case 2: 7
Hint
Source
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
using namespace std;
int n,m,set[50010];
int find(int x)
{
if(x==set[x])
return x;
return find(set[x]);
}
void combine(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
set[fy]=fx;
}
int main()
{
int con=0;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(m==0&&n==0)
break;
con++;
memset(set,0,sizeof(set));
for(int i=1;i<=n;i++)
set[i]=i;
while(m--)
{
int x,y;
scanf("%d%d",&x,&y);
combine(x,y);
}
int ans=0;
for(int i=1;i<=n;i++)
if(set[i]==i)
ans++;
printf("Case %d: %d\n",con,ans);
}
//system("pause");
return 0;
}
标签:des blog io ar os sp for strong div
原文地址:http://www.cnblogs.com/a972290869/p/4101080.html