POJ 2492-A Bug's Life(帶權並查集)

kewlgrl發表於2017-05-04
A Bug's Life
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 36820   Accepted: 12027

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.

Source

TUD Programming Contest 2005, Darmstadt, Germany

題目意思:

給出N個飛蛾之間的異性關係,判斷是否存在同性戀。

解題思路:

每隻飛蛾看作一個節點,0/1標記當前飛蛾節點和根節點的同異性關係。
每次輸入一組就進行處理,同一棵樹中的兩個點根據和根節點的關係判斷同異性關係,否則合併後更新同異性關係。

#include<iostream>
#include<cstdio>
#include<iomanip>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<map>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define INF 0xfffffff
#define MAXN 10010
int fa[MAXN];
int a[MAXN],b[MAXN];
bool rel[MAXN];//0/1標記和樹根的同異性關係
int setfind(int x)
{
    int temp=fa[x];
    if(temp==x) return x;
    fa[x]=setfind(temp);
    rel[x]=(rel[x]+rel[temp])%2;//更新同異性關係
    return fa[x];
}

void join(int p,int q)
{
    int x=setfind(p);
    int y=setfind(q);
    if(x!=y) fa[x]=y;
    rel[x]=((rel[p]-rel[q]+1)%2);//確定同異性關係,如果之前是同性則改為異性
}

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("G:/cbx/read.txt","r",stdin);
    //freopen("G:/cbx/out.txt","w",stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t,ca=0;
    cin>>t;
    while(t--)
    {
        for(int i=0; i<MAXN; ++i) fa[i]=i;
        memset(rel,false,sizeof(rel));
        int n,m;
        bool flag=false;
        cin>>n>>m;
        for(int i=0; i<m; ++i)
        {
            int a,b;
            cin>>a>>b;
            if(setfind(a)==setfind(b))
            {
                if(rel[a]==rel[b]%2)
                    flag=true;//出現同性戀
            }
            else join(a,b);
        }
        if(flag) cout<<"Scenario #"<<++ca<<":"<<endl<<"Suspicious bugs found!"<<endl<<endl;
        else cout<<"Scenario #"<<++ca<<":"<<endl<<"No suspicious bugs found!"<<endl<<endl;
    }
    return 0;
}


相關文章