poj 2492 A Bug's Life

A Bug's Life
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 20641   Accepted: 6709

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

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

Sample Output

Scenario #1:
Suspicious bugs found!

Scenario #2:
No suspicious bugs found!

Hint

Huge input,scanf is recommended.
 
 
加了偏移的并查集,跟前面那个犯罪团伙的题几乎一样
关键是要推出性别关系的表达式,列举几种情况就可以推出来了
同时在查找父节点和合并两个集合的时候注意更新即可
 
#include<iostream>
using namespace std;

int father[2010];
int rank[2010];

void make_set(int n)
{
    for(int i=1;i<=n;i++)
    {
        father[i]=i;
        rank[i]=0;
    }
}
int find_father(int n)
{
    int temp = father[n];
    if(n!=father[n])
        father[n]=find_father(father[n]);
    rank[n]=(rank[n]+rank[temp])%2;
    return father[n];
}
void union_set(int a, int b)
{
    int fa = find_father(a);
    int fb = find_father(b);
    if(fa!=fb)
    {
        father[fa]=fb;
        rank[fa] = (rank[a]+rank[b]+1)%2;
    } 
}

int main()
{
    int cases;
    int i;
    int x, y;
    int n, m;
    int flag = 0;
    freopen("e:\\data.txt", "r", stdin);        
    freopen("e:\\out.txt", "w", stdout); 
    cin>>cases;
    for(i=1;i<=cases;i++)
    {
        cin>>n>>m;
        make_set(n);
        int flag = 0;
        while(m--)
        {
            cin>>x>>y;
            union_set(x, y);
            if(flag == 1)
                continue;
            int fa = find_father(x);
            int fb = find_father(y);
            if(fa == fb && rank[x]==rank[y])
                flag=1;
        }
        cout<<"Scenario #"<<i<<":"<<endl;
        if(flag == 1)
            cout<<"Suspicious bugs found!"<<endl<<endl;
        else
            cout<<"No suspicious bugs found!"<<endl<<endl;
    }
    return 0;
}

 

posted @ 2012-05-14 21:41  w0w0  阅读(188)  评论(0)    收藏  举报