A Bug's Life

来源:互联网 发布:淘宝免费充话费 编辑:程序博客网 时间:2024/05/16 01:37

A Bug's Life

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15110    Accepted Submission(s): 4913


Problem Description
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.
题目意思:给定一系列数对,例如a和b,表示a和b不是同一种性别,然后不断的给出这样的数对,问有没有性别不对的情况。
例如给定:
1    2
3    4
1    3
那这里就是说1和2不是同种性别,3和4也不是同种性别,1和3不是同种性别,那这样就说明1和3是同一种性别,2和4是同一种性别,所以没有任何歧义,这时候输出No suspicious bugs found!
但是例如,(本文作者:CSDN:凌风)
1 2
2 3
1 3
1和2不同性别,2和3不同性别,那么1和3同一性别的,但是第三组数对又表明1和3不同性别,所以这里就出现了3或者1的性别出现了歧义,也就是说条件矛盾,这时候输出Suspicious bugs found!.

总共存在两个帮派,有两种操作D [a] [b] 说明a,b属于不同的帮派,A [a] [b] 需要我们来判断他们的关系,属于同一帮派输出,属于不同的帮派,或者不确定。

关系并查集

利用并查集构成的树的长度关系,我们把不同的利用并查集合并,然后维持一个他们到根节点的距离dep我们只要判断他们的距离的差值是否为2的倍数就可判断时代否在同以帮派了。

#include<stdio.h>#include<iostream>#include<string.h>#include<algorithm>using namespace std;int father[2005];int dep[2005];int n,m;int a[1000009],b[1000009];int abs(int a){    if(a<0)        return -a;    else        return a;}void init(){    for(int i=0; i<=n; i++)    {        father[i]=i;        dep[i]=0;    }}int getfather(int x){    if (x != father[x])    {        int tmp = father[x];        father[x] =getfather(father[x]);        dep[x] = (dep[tmp] + dep[x])%2;        return father[x];//加上这个,否则会超内存    }    return x;}void we(int x1,int y1){    int x=getfather(x1);    int y=getfather(y1);    if(x!=y)    {        father[y]=x;        dep[y] = (dep[x1] + dep[y1] + 1)%2;    }}bool same(int x,int y){    return getfather(x)==getfather(y);}int main(){    int t;    scanf("%d",&t);    int g=0;    while(t--)    {        ++g;        scanf("%d%d",&n,&m);        init();        for(int i=0; i<m; i++)        {            scanf("%d%d",&a[i],&b[i]);            we(a[i],b[i]);        }        int p=0;        for(int i=0; i<m; i++)        {            if(same(a[i],b[i])&&abs(dep[a[i]]-dep[b[i]])%2==0)            {                p=1;                break;            }        }        printf("Scenario #%d:\n",g);        if(p==1)            printf("Suspicious bugs found!\n\n");        else            printf("No suspicious bugs found!\n\n");    }}


0 0