NEU 1293: Gang Black and Gang White 并查集

题目描述

 There are only two gangs in the city, Gang Black and Gang White. There are N persons(2<=N<=50000)  who belong to these two gangs(one person only belongs to one gang). They are numbered from 1 to N.
   As a policeman, you are given M pieces of messages. Each message has two integers A and B. It means that person A and person B are not in the
same gang.
   Now you have to judge whether there is something wrong with these M pieces of messages. If you find that there is someone belongs to these two gangs at the same time through these messages, you will think there is 
something wrong with these messages.  

开个against表示敌人来记录一下就能AC了 
/*
 * @author :ipqhjjybj
 */
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
int N,M;
int V[50011];
int against[50011];
int tree(int i)
{
    if(V[i] == 0){
        V[i] = i;
        return i;
    }
    if(V[i] == i){
        return i;
    }
    V[i] = tree(V[i]);
    return V[i];
}
void comb(int a,int b){
    V[b] = tree(a);
    V[tree(b)] = tree(a);
}
int main()
{
    int test,j;
    int i,a,b;
    int right;
    //freopen("test.out","r",stdin);
    scanf("%d",&test);
    for(j = 1;j <= test;j++){
        memset(V,0,sizeof(V));
        memset(against,0,sizeof(against));
        scanf("%d %d",&N,&M);
        right = 1;
        for(i = 1;i <= M;i++){
            scanf("%d %d",&a,&b);
            if(tree(a) == tree(b)){
                for(i++;i <= M;i++)
                    scanf("%d %d",&a,&b);

                right = 0;
                break;
            }
            else{
                if(against[b] != 0){
                    comb(a,against[b]);
                    against[b] = tree(a);
                }else{
                    against[b] = tree(a);
                }
                if(against[a] != 0){
                    comb(against[a],b);
                }else{
                    against[a] = tree(b);
                }
            }
        }
        if(right){
            printf("Test case #%d:\n",j);
            printf("Nothing special.\n");
            if(j != test)
                printf("\n");
        }
        else{
            printf("Test case #%d:\n",j);
            printf("Something wrong!\n");
            if(j != test)
                printf("\n");
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值