[Luogu P2024 & P1892] & food chain gangs (disjoint-set to expand the domain)

P1892 Luogu
Luogu P2024
two road at first glance it is easy to find and can be used to check set to do - but when we face carefully read the title, will find that in fact is not so simple.
We know disjoint-set can easily maintain information transitive, that is, "friends of friends are my friends," such a relationship, but ordinary disjoint-set and can not maintain the "enemy of my enemy is a friend" relationship .
This time we will introduce a magical operation, and set to double check, we will increase this space to double the maintenance enemy node i.
For example, the gang problem

    if (c=='F')
        {
            merge(x,y);
        }
        else 
        {
            merge(x,y+n);
            merge(y,x+n);
            //x+n代表的是x的敌人集合
        }

Very easy to understand, really is a very efficient operation.

P1892 complete code

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=2001;
int fa[maxn],n,m,cnt,x,y;
int getf(int v)
{
    if (fa[v]==v) return fa[v];
    return fa[v]=getf(fa[v]);
}
inline void merge(int x,int y)
{
    fa[getf(y)]=getf(x);
}
inline bool check(int x,int y)
{
    if (getf(x)==getf(y)) return true;
    return false;
}
int main()
{
    scanf("%d%d",&n,&m);
    for (int i=1;i<=2*n;i++) fa[i]=i;
    for (int i=1;i<=m;i++)
    {
        char c;
        scanf("\n%c %d %d",&c,&x,&y);
        if (c=='F')
        {
            merge(x,y);
            //merge(x+n,y+n);
            //注意上面这句不能要,因为题目中没有说到朋友的敌人也是我的敌人。
        }
        else 
        {
            merge(x,y+n);
            merge(y,x+n);
        }
    }
    for (int i=1;i<=n;i++) fa[i]=getf(i);//路径压缩,防止压缩不完全
    for (int i=1;i<=n;i++) if (fa[i]==i) cnt++;
    printf("%d\n",cnt);
    return 0;
}

The complete code P2024

#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=3*5*1e4+10;
int fa[maxn],cnt,n,k,flag,x,y;
int getf(int v)
{
    if (fa[v]==v) return v;
    return fa[v]=getf(fa[v]);
}
inline void merge(int x,int y)
{
    x=getf(x);
    y=getf(y);
    fa[x]=y;
}
inline bool check(int x,int y)
{
    x=getf(x);
    y=getf(y);
    if (fa[x]==fa[y]) return true;
    return false;
}
int main()
{
    scanf("%d%d",&n,&k);
    for (int i=1;i<=3*n;i++)
        fa[i]=i;
    //i+n是i吃的集合,i+2n是吃i的集合
    for (int i=1;i<=k;i++)
    {
        scanf("%d%d%d",&flag,&x,&y);
        if (x>n||y>n) {cnt++;continue;}
        if (flag==1)
        {
            if (check(x+n,y)) {cnt++;continue;}
            if (check(x+2*n,y)) {cnt++;continue;}
            merge(x,y);
            merge(x+n,y+n);
            merge(x+2*n,y+2*n);
        }
        if (flag==2)
        {
            if (check(x,y)) {cnt++;continue;}
            if (check(y+n,x)) {cnt++;continue;}
            merge(x+n,y);
            merge(y+2*n,x);
            merge(x+2*n,y+n);
        }
    }
    printf("%d\n",cnt);
    return 0;
}

Guess you like

Origin www.cnblogs.com/notscience/p/11828321.html