C - 食物链

https://vjudge.net/contest/280495#problem/C

#include
#include
#include
using namespace std;
int n,m,i,j,k,t,z,l,x1,y1,a[300000],x,y,f[300000],ans;
int find(int x)
{
    if (f[x]==x) return x;
    return f[x]=find(f[x]);
}
int main()
{
    scanf("%d%d",&n,&k);
    for (i=1; i<=n*3; i++)
        f[i]=i;
    for (i=1; i<=k; i++)
    {
        scanf("%d%d%d",&z,&x,&y);
        int x_eat=n+x,x_emeny=n*2+x,y_eat=n+y,y_emeny=n*2+y;
        if ((z==2&&x==y)||x>n||y>n) {
            ans++;
            continue;
        }
        if (z==1)
        {
            if (find(x)==find(y_eat)||find(x_eat)==find(y)) ans++;else{
                f[find(x_eat)]=find(y_eat);
                f[find(x)]=find(y);
                f[find(x_emeny)]=find(y_emeny);
            }
        }
        else
        {
            if (find(x)==find(y)||find(x_emeny)==find(y)) ans++;else{
                f[find(x_eat)]=find(y);
                f[find(y_emeny)]=find(x);
                f[find(x_emeny)]=find(y_eat);
            }
        }
    }
    printf("%d\n",ans);
}

 

你可能感兴趣的:(C - 食物链)