种类并查集(真)洛谷p1892团伙(并查集+反集)

链接:https://www.luogu.com.cn/problem/P1892

建立自己的一个虚拟的敌人与对方形成朋友从而间接的把敌人的敌人和自己连接到一个并查集里,

种类并查集裸题:

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
#define maxx 1010
#define maxn 50010
int fa[maxn],va[maxn];
int Find(int x)
{
    return x==fa[x]?x:fa[x]=Find(fa[x]);
}
void Union(int x,int y)
{
    int fx=Find(x);
    int fy=Find(y);
    if(fx!=fy)
    {
        fa[fx]=fy;
    }
}
int main()
{
        int n,m;
        cin>>n>>m;
        for(int i=0; i<=2*n; i++)
             fa[i]=i;
        while(m--)
        {
            char ch;
            int x,y;
            cin>>ch>>x>>y;
            if(ch=='F')
                Union(x,y);
            else
            {//合并敌人的敌人
                Union(y+n,x);
                Union(x+n,y);
            }
        }
        int ans=0;
        for(int i=1; i<=n; i++)
            if(fa[i]==i)
              ans++;
        cout<<ans<<endl;
    return 0;}
原文地址:https://www.cnblogs.com/sweetlittlebaby/p/12656509.html