标签:假设 思想 带权并查集 play iostream rip com 集合 space
【\(Description\)】https://www.luogu.com.cn/problem/P2024
\(Code:\)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=50010;
int n,K,f[N],d[N],res;
int find(int x)
{
    if(f[x]!=x)
    {
        int root=find(f[x]);
        d[x]=(d[x]+d[f[x]])%3;
        f[x]=root;
    }
    return f[x];
}
int main()
{
    scanf("%d%d",&n,&K);
    for(int i=1;i<=n;i++)f[i]=i;
    while(K--)
    {
        int opt,x,y;
        scanf("%d%d%d",&opt,&x,&y);
        if(x>n||y>n)
        {
            res++;
            continue;
        }
        if(opt==1)
        {
            int p=find(x),q=find(y);
            if(p==q)
            {
                if(d[x]!=d[y])
                {
                    res++;
                    continue;
                }
            }
            else 
            {
                f[p]=q;
                d[p]=(3-d[x]+d[y])%3;
            }
        }
        else
        {
            int p=find(x),q=find(y);
            if(p==q)
            {
                if(d[x]==d[y]||d[x]!=(d[y]+1)%3)
                {
                    res++;
                    continue;   
                }
            }
            else
            {
                f[p]=q;
                d[p]=(4-d[x]+d[y])%3;
            }
        }
    }
    printf("%d",res);
    return 0;
}
标签:假设 思想 带权并查集 play iostream rip com 集合 space
原文地址:https://www.cnblogs.com/czyty114/p/12695837.html