链接:https://www.luogu.com.cn/problem/P1892
由于敌人的敌人是朋友,因此可以设自己的一个虚拟的敌人
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int fa[10010];
int find(int x)
{
return x==fa[x]?x:fa[x]=find(fa[x]);
}
void Union(int x,int y)
{
int fx=find(x),fy=find(y);
if(fx!=fy)
fa[fx]=fy;
return;
}
int main()
{
int n,m;
cin>>n>>m;
for(int i=1; i<=2*n; i++) fa[i]=i;
for(int i=1; i<=m; i++)
{
char ch;
int p,q;
cin>>ch>>p>>q;
if(ch=='F')
Union(p,q);
else
{
Union(q+n,p);
Union(p+n,q);
}
}
int ans=0;
for(int i=1; i<=n; i++)
{
if(fa[i]==i)
ans++;
}
cout<<ans<<endl;}