Find them, Catch them--POJ 1703带权并查集

22 篇文章 0 订阅

题意:给出T个测试样例

N个人  M组数据

A表示查询,D表示增加关系

 

#include <iostream>
#include <stdio.h>
using namespace std;
int father[100050];
int relation[100050];


void init(int n)
{
    for(int i=1;i<=n;i++)
    {
        father[i]=i;
        relation[i]=0;


    }
}
int find(int x)
{
    if(x!=father[x])
    {
        int tmp=father[x];
        father[x]=find(tmp);
        relation[x]=(relation[x]+relation[tmp])%2;
    }
    return father[x];
}
void Union(int x,int y)
{
    int root1=find(x);
    int root2=find(y);
    father[root2]=root1;
    relation[root2]=(-relation[y]+1+relation[x])%2;


}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n,m;
        cin>>n>>m;
        init(n);
        while(m--)
        {
        char s[5];
        int x,y;
        scanf("%s",s);
        scanf("%d%d",&x,&y);
        if(s[0]=='D')
        {
            Union(x,y);
        }
        else
        {   int fa,fb;
            fa=find(x);
            fb=find(y);
            if(fa!=fb)
            {
                printf("Not sure yet.\n");
            }
            else
            {
                if(relation[x]==relation[y])
                {
                    printf("In the same gang.\n");
                }
                else
                {
                    printf("In different gangs.\n");
                }
            }
        }
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值