H - Por Costel and the Match -种类并查集

  • H - Por Costel and the Match

  •  Gym - 100923H 
  • #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define maxn 2000005
    int pre[maxn];
    int t,n,m,u,v;
    struct node
    {
        int pos,same;
    } x,y;
    node fond(int x)
    {
        node ans;
        if(pre[x]==0)
        {
            ans.same=1;
            ans.pos=x;
            return ans;
        }
        else if(pre[x]>0)
        {
            ans=fond(pre[x]);
            if(ans.same)pre[x]=ans.pos;
            else pre[x]=-ans.pos;
            return ans;
        }
        else
        {
            ans=fond(-pre[x]);
            if(ans.same)pre[x]=-ans.pos;
            else pre[x]=ans.pos;
            ans.same=!ans.same;
            return ans;
        }
    }
    int main()
    {
        freopen("meciul.in","r",stdin);
        freopen("meciul.out","w",stdout);
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%d",&n,&m);
            for(int i=1; i<=n; i++)
                pre[i]=0;
            while(m--)
            {
                scanf("%d%d",&u,&v);
                x=fond(u);
                y=fond(v);
                if(x.pos==y.pos)
                {
                    if(x.same==y.same)
                        printf("NO\n");
                    else
                        printf("YES\n");
                    continue;
                }
                if(x.same==y.same)
                    pre[x.pos]=-y.pos;
                else
                    pre[x.pos]=y.pos;
                printf("YES\n");
            }
        }
        return 0;
    }
    
  •  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值