食物链

· · 个人记录

#include<bits/stdc++.h>
#define MAXN 150002
#define A (0) 
#define B (n)
#define C (n*2)
using namespace std;
int n,k,fa[100010],lies;
int find_set(int x)
{
    if(fa[x]!=x)
    {
        return fa[x]=find_set(fa[x]);
    }
    return x;
}
void union_set(int x,int y)
{
    fa[find_set(x)]=find_set(y);
}
int main()
{
    cin>>n>>k;
    for(int i=1;i<=3*n;i++)
    {
        fa[i]=i;
    }
    while(k--)
    {
        int op,x,y;
        cin>>op>>x>>y;
        if(x>n||y>n)
        {
            lies++;
            continue;
        }
        if(op==1)
        {
            if(find_set(x+A)==find_set(y+B)||find_set(x+A)==find_set(y+C))
            {
                lies++;
            }
            else
            {
                union_set(x+A,y+A),union_set(x+B,y+B),union_set(x+C,y+C);
            }
        }
        else
        {
            if(find_set(x+A)==find_set(y+A)||find_set(x+A)==find_set(y+C))
            {
                lies++;
            }
            else
            {
                union_set(x+A,y+B),union_set(x+B,y+C),union_set(x+C,y+A);
            }
        }
    }
    cout<<lies;
    return 0;
}