poj 1182 食物链【带权并查集】

设相等的边权为0,吃的边权为,被吃的边权为2,然后用带权并查集在%3的意义下做加法即可
关系为简单环的基本都可以用模环长的方式是用带权并查集

#include<iostream>
#include<cstdio>
using namespace std;
const int N=50005;
int n,m,f[N],s[N],ans;
int read()
{
    int r=0,f=1;
    char p=getchar();
    while(p>'9'||p<'0')
    {
        if(p=='-')
            f=-1;
        p=getchar();
    }
    while(p>='0'&&p<='9')
    {
        r=r*10+p-48;
        p=getchar();
    }
    return r*f;
}
int zhao(int x)
{
    if(x==f[x])
        return x;
    int nw=zhao(f[x]);
    s[x]=(s[x]+s[f[x]]+3)%3;
    return f[x]=nw;
}
int main()
{
    n=read(),m=read();
    for(int i=1;i<=n;i++)
        f[i]=i;
    while(m--)
    {
        int d=read()-1,x=read(),y=read();
        if(x>n||y>n||(d==1&&x==y))
        {
            ans++;
            continue;
        }
        int fx=zhao(x),fy=zhao(y);
        if(fx!=fy)
        {
            f[fy]=fx;
            s[fy]=(s[x]-s[y]+d+3)%3;
        }
        else if((s[y]-s[x]+3)%3!=d)
            ans++;
    }
    printf("%d\n",ans);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/lokiii/p/9970305.html