借着给大二出题的机会更...
好久没刷题了
我在想我到底适不适合算法
并查集新手入门就做这道题
/*
qq:1239198605
ctgu_yyf
*/
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<set>
#define ll long long
using namespace std;
int par[50005];
int rankk[50005];
int n,m;
void init(int n)
{
for(int i=1;i<=n;i++)
{
par[i]=i;
rankk[i]=0;
}
}
int find(int x)
{
if(par[x]==x)
return x;
else
return par[x]=find(par[x]);
}
bool same(int u,int v)
{
return find(u)==find(v);
}
void unite(int x,int y)
{
x=find(x);
y=find(y);
if(x==y)
return ;
if(rankk[x]>rankk[y])
{
par[x]=y;
}
else
{
par[y]=x;
if(rankk[x]==rankk[y])
rankk[x]++;
}
}
int main()
{
ios::sync_with_stdio(false);
int k=1;
while(cin>>n>>m)
{
if(n==0&&m==0)
break;
init(n);
int u,v;
while(m--)
{
cin>>u>>v;
if(same(u,v)==false)
unite(u,v);
}
int ans=0;
for(int i=1;i<=n;i++)
{
if(i==par[i])
ans++;
}
cout<<"Case "<<k<<": ";
cout<<ans<<endl;
k++;
}
return 0;
}