poj 2492 A BUG'S LIFE 不是纯裸的并查集

来源:互联网 发布:淘宝原创男包品牌 知乎 编辑:程序博客网 时间:2024/05/18 03:50

Background
Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their backs.

Problem
Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it.
 

Input

The first line of the input contains the number of scenarios. Each scenario starts with one line giving the number of bugs (at least one, and up to 2000) and the number of interactions (up to 1000000) separated by a single space. In the following lines, each interaction is given in the form of two distinct bug numbers separated by a single space. Bugs are numbered consecutively starting from one.
 

Output

The output for every scenario is a line containing "Scenario #i:", where i is the number of the scenario starting at 1, followed by one line saying either "No suspicious bugs found!" if the experiment is consistent with his assumption about the bugs' sexual behavior, or "Suspicious bugs found!" if Professor Hopper's assumption is definitely wrong.
 

Sample Input

23 31 22 31 34 21 23 4
 

Sample Output

Scenario #1:Suspicious bugs found!Scenario #2:No suspicious bugs found!

Hint

Huge input,scanf is recommended. 
题意:就是给你一个一群编号从1-n的虫子,然后给出一些关系表示相互间是不同性别,让你判断有没有矛盾,有矛盾就输出Suspicious bugs found!,没有就输出No suspicious bugs found!举个例子吧:第一个案列就是说有三只虫子,给出了三种关系,1和2 是不同性别,2和3是不同性别,我们可以知道1和3是相同性别,但给出的是1和3是不同性别产生了矛盾就有bugl输出就好了,第二个案例就是说1,2是不同性别,3,4是不同性别显示没有矛盾。
思路:我们需要两个数组,一个用来存放虫子所属的分组,一个用来存虫子的性别,首先我们假定每个虫子都在不同的分组,每个虫子与自己都是同性用0表示,当异性在一起的时候,把他们放在同一个集合里面,其中一个变为性别1,然后在最后判断的时候看看给定的两个虫子在不在同一个分组里面,在同一个分组里 如果两个的性别相同的话那就是有矛盾了。
做的太单纯,错了好多次,后来想清楚了还是错,给自己一组测试数据没过3 3 1 2 1 2 1 2 答案 No suspicious bugs found!结果就是不对啊,后来才意识到就是需要标记每个虫子的性别,然而并不懂怎么做,看了博客,就知道了怎样来做。
总结:做题想的深一点,不要太简单的去做它,这道题收获了不少。
AC代码:
#include <iostream>#include <cstdio>
using namespace std;
int fu[2010];//他的父亲的状态int zi[2010];//自己的状态
int found(int x){//找当前孩子的父亲    int r ;    if(x == fu[x]){        return x;    }        r = fu[x] ;        fu[x] = found(fu[x]);        zi[x] = (zi[x] + zi[r] ) % 2;//孩子的父亲改变了,就要更新孩子的性别    return fu[r];}
void bing(int x,int y){    int xx = found (x);    int yy = found (y);    fu[yy] = xx;    zi[yy] = (zi[x] - zi[y] + 1) % 2;//合并两个虫子,在同一个分组,标记两个孩子性别不同}
int main(){    int t;    int bug;    int guanxi;    scanf("%d",&t);    for(int j = 1;j <= t ;j++){        scanf("%d%d",&bug,&guanxi);        for(int i = 1; i <= bug; ++i){            fu[i] = i;            zi[i] = 0;        }        int flag = 0;        int bug1,bug2;        while(guanxi--){            scanf("%d%d",&bug1,&bug2);
                if(found ( bug1 ) == found (bug2)){                    if(zi[bug1] == zi[bug2]){                        flag = 1;                    }                }                else{                    bing(bug1,bug2);                }        }        printf("Scenario #%d:\n",j);
        if(flag == 0){            printf("No suspicious bugs found!\n\n");        }        else{            printf("Suspicious bugs found!\n\n");        }    }    return 0;}附 组测试数据
3 3
1 2
1 2
1 2
很重要的         
 
0 0