题目描述
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;
}