RE求助
查看原帖
RE求助
1048319
封禁用户楼主2024/7/24 17:39
#include<bits/stdc++.h>
using namespace std;
long long n,m,x,y,fa[1005],cnt;
void init()
{
	for(long long i=1;i<=n;i++)
	    fa[i]=i;
}
int root(long long x)
{
	return x=x==fa[x]?x:root(fa[x]);
}
int merge(long long x,long long y)
{
	long long fx,fy;
	fx=root(x);
	fy=root(y);
	if(x!=y)
		fa[fx]=fy;
	cnt++;
	    
}
int main()
{
	while(1)
	{
		scanf("%lld",&n);
		if(n==0) break;
		scanf("%lld",&m);
		init();
		for(int i=0;i<m;i++)
		{
			scanf("%lld%lld",&x,&y);
			merge(x,y);
		}
		if(cnt<n-1)
			cnt=n-1-cnt;
		printf("%lld\n",cnt);
		cnt=0;
	}
	return 0;
} 
2024/7/24 17:39
加载中...