1227: Gang Black and Gang White

来源:互联网 发布:炫舞按键精灵源码 编辑:程序博客网 时间:2024/05/17 23:44

题目描述

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.

输入

The first line contains an integer T(1<=T<=20),the number of cases.
Then T cases follow. Each test case begins with a line with two integers N and M(0<=M<=50000). And then, each of the following M messages contains two integers A and B per line. (1<=A,B<=N)

输出

The output for every test case is a line containing “Test case #i:”, where i is the number of the test case starting at 1, followed by one line saying either “Something wrong!” if you find out a man belongs to two gangs in the same time, or “Nothing special.” if you don’t find something wrong.
There is a blank line after each test case.

样例输入

3
4 2
2 1
4 3
3 3
1 2
1 3
2 3
5 1
1 2

样例输出

Test case #1:
Nothing special.

Test case #2:
Something wrong!

Test case #3:
Nothing special.

解答

#include<stdio.h>int gang[50001];int level=0,next=0;int father(int b){    level=0;next=b;    while(gang[b]!=b) {next=b;b=gang[b];level++;}    return b;}int main(){    int T,N,M,i,j,a,b,sign,f1,l1,l2,f2,n1,n2;    scanf("%d",&T);    for(j=1;j<=T;j++)    {        scanf("%d%d",&N,&M);        for(i=1;i<=N;i++)             gang[i]=i;        sign=0;        while(M--)        {            scanf("%d%d",&a,&b);            if(a==b || a<1 || b<1 || a>N || b>N) {sign=1;break;}            f1=father(a);l1=level;n1=next;f2=father(b);l2=level;n2=next;            if(f1==f2 && (l1+l2)%2==0) {sign=1;break;}            if(f1!=f2)             {                if(l1==l2) gang[f2]=f1;                if(l1<l2) {if((l2+l1)%2==0) gang[f1]=f2;else gang[f1]=n2;}                if(l1>l2) {if((l1+l2)%2==0) gang[f2]=f1;else gang[f2]=n1;}            }        }        printf("Test case #%d:\n",j);        if(sign==0) printf("Nothing special.\n\n");        if(sign==1) printf("Something wrong!\n\n");        while(M>0)        {scanf("%d%d",&a,&b);M--;}    }    return 0;}
0 0