poj-1703 Find them, Catch them!! 并查集

题意就不多说了,要注意一下当N=2时一定是属于两个不同帮派的,而不是不确定。

看起来很简单的并查集,我愣是作了一晚上,最后才发现递归写错了大哭

思路大体是这样的,对每一对并在一起的犯罪团伙,记录下他与根节点的距离,当询问时,只要看看这两个的根节点是否相同就能判断是否已经确定,让后根据与根节点的距离来判断是否是同一帮派

#include<stdio.h>
#include<string.h>
struct s
{
    int fa;
    int val;
};
s map[100010];
int fi(int x)   //递归寻找根节点的过程中要更新所有关联的节点
{

    if(map[x].fa==x) return x;    
    int temp=map[x].fa;     
    map[x].fa=fi(map[x].fa);
    map[x].val=(map[temp].val+map[x].val)%2; 
    return map[x].fa;
}
int main()
{
    int T,n,m,i,x,y,flag,cnt1,cnt2;
    char c;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        if(n==2)
        {

            for(i=1; i<=m; i++)
            {
                getchar();
                scanf("%c%d%d",&c,&x,&y);
                if(c=='A')
                {
                    if(x==y) printf("In the same gang.\n");
                    else printf("In different gangs\n");
                }
            }
        }
        else
        {
            for(i=1; i<=n; i++)
            {
                map[i].fa=i;
                map[i].val=0;
            }
            for(i=1; i<=m; i++)
            {
                getchar();
                scanf("%c%d%d",&c,&x,&y);
                if(c=='D')
                {
                    cnt1=fi(x);
                    cnt2=fi(y);
                    if(cnt1<cnt2)
                    {
                        map[cnt2].fa=cnt1;
                        map[cnt2].val+=(map[y].val+1+map[x].val);  //记下与根节点的距离
                    }
                    else
                    {
                        map[cnt1].fa=cnt2;
                        map[cnt1].val+=(map[y].val+map[x].val+1);
                    }
                //   for(int j=1;j<=n;j++)
                  //  {
                  //    printf("%d %d %d\n",j,map[j].fa,map[j].val);
                //   }
                }
                if(c=='A')
                {
                    cnt1=x;
                    cnt2=y;
                    x=fi(x);
                    y=fi(y);
                     cnt1=map[cnt1].val;
                    cnt2=map[cnt2].val;


                  //    for(int j=1;j<=n;j++)
                 //  {
                     //printf("%d %d %d\n",j,map[j].fa,map[j].val);
                  // }


                    if(x==y)
                    {
                        if((cnt1%2)==(cnt2%2))
                            printf("In the same gang.\n");
                        else
                            printf("In different gangs.\n");
                    }
                    else printf("Not sure yet.\n");
                }
            }
        }
    }

    return 0;
}


/*1
11 30
A 1 2
Not sure yet.
D 1 3
D 4 5
A 1 4
Not sure yet.
D 3 5
A 1 4
In different gangs.
A 1 5
In the same gang.
D 3 5
D 7 9
D 2 7
A 1 2
Not sure yet.
D 1 7
A 1 9
In the same gang.
A 3 7
In the same gang.
A 5 7
In different gangs.
A 2 5
In the same gang.
A 2 4
In different gangs.
A 9 5
In the same gang.
D 8 10
D 6 8
A 6 10
In the same gang.
D 10 11
A 8 11
In the same gang.
D 1 8
A 1 11
In different gangs.
A 4 11
In the same gang.
A 9 11
In different gangs.
A 2 6
In the same gang.
A 3 6
In different gangs.
A 1 1
<pre name="code" class="cpp">In the same gang.


1
4 4
D 1 2
D 3 4
D 1 3
A 1 4
<pre name="code" class="cpp">In the same gang.

 
 下面是在网上找的大神的测试样例. 


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值