查看原帖
475575
_001楼主2021/3/7 10:39
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int a[1000],father[1000],tot;
int find(int x)
{
	if(father[x]!=x) 
	father[x]=find(father[x]);
	return father[x];
}
int hb(int x,int y)
{
	int a=find(x);
	int b=find(y);
	father[x]=y;
}
int main()
{
	int n,m;
	int x,y;
	char s;
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	father[i]=i;
	for(int i=1;i<=m;i++)
	{
		cin>>s>>x>>y;
		if(s=='F')
		hb(x,y);
		else {	
			if(!a[x])
			{
				a[x]=y;
			}
			else 
				hb(a[x],y);
			if(!a[y])
				a[y]=x;
			else 
				hb(a[y],x);
		}
	}
	for(int i=1;i<=n;i++)
	if(father[i]==i)
	tot++;
	cout<<tot;
}
2021/3/7 10:39
加载中...