POJ - 1611 The Suspects

https://vjudge.net/problem/POJ-1611

#include<iostream>
#include<algorithm>
using namespace std;
const int N=50010;
int n,m;
int p[N];
int find(int x)
{
    
    
	if(p[x]!=x) p[x]=find(p[x]);
	return p[x];
}
int main()
{
    
    
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int cnt=1;
	while(cin>>n>>m)
	{
    
    
		if(n==0 || m==0)
			break;
		for(int i=1;i<=n;i++) p[i]=i;
		int a,b,t1,t2;
		while(m--)
		{
    
    
			cin>>a>>b;
			t1=find(a),t2=find(b);
			if(t1!=t2)
				p[t1]=t2;
		}
		int ans=0;
		for(int i=1;i<=n;i++)
			if(p[i]==i)
				ans++;
		cout<<"Case "<<cnt++<<": "<<ans<<"\n";
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_52341477/article/details/119978145