poj 1703 Find them, Catch them

还是种类并查集!!


题目链接:

点击打开链接

题目大意:

给出不属于同一集合的人的关系。给出询问并判断是不是同一个组织或者当前信息不能判断

其关系。


#include<cstdio>
#include<iostream>
using namespace std;

int fa[100010];
int re[100010];

void init(int n)
{
    for(int i=1;i<=n;i++)
    {
        fa[i]=i;
        re[i]=0;
    }
}

int find(int x)
{
    if(fa[x]!=x)
    {
        int t=fa[x];
        fa[x]=find(fa[x]);
        re[x]=(re[x]+re[t])%2;
    }
    return fa[x];
}

void Union(int x,int y)
{
    int fx=find(x);
    int fy=find(y);
    fa[fx]=fy;
    re[fx]=(re[y]+2-re[x]+1)%2;
}

int main()
{
    int T,n,m,x,y;
    char u;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        init(n);

        getchar();
        for(int i=0;i<m;i++)
        {
            scanf("%c%d%d",&u,&x,&y);
            if(u=='D')
            {
                Union(x,y);
            }
            else
            {
                int fx=find(x);
                int fy=find(y);

                if(fx!=fy)
                    printf("Not sure yet.\n");
                else
                {
                    if((re[x]+re[y])%2)
                        printf("In different gangs.\n");
                    else
                        printf("In the same gang.\n");
                }
            }
            getchar();
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值