1. 程式人生 > >HDU1289 A Bug's Life (帶權並查集)

HDU1289 A Bug's Life (帶權並查集)

clu some assume ever different 合並操作 ports main separate

HDU1289 帶權並查集

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

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. 

題解

題意

就是給出bug的關系,一組bug性別不同,問是否出現矛盾?

思路

帶權並查集基本題目,整理模板。通過gender關系可以對父節點與子節點進行運算。合並操作時的關系運算可以參考:

https://blog.csdn.net/sunmaoxiang/article/details/80959300

代碼

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;

const int MAXN = 2e3+10;
int fat[MAXN],gender[MAXN];

void init(){
    for(int i=0;i<MAXN;i++){
        fat[i] = i;
        gender[i]=0;
    }
}

int find(int x){
    if(x==fat[x])   return x;
    int t = find(fat[x]);
    gender[x] = gender[x]^gender[fat[x]];
    return fat[x] = t;
}

bool merge(int x,int y){
    int fx = find(x);
    int fy = find(y);
    if(fx==fy){
        if(gender[x]==gender[y])    return  true;
        return false;
    }
    fat[fx] = fy;
    gender[fx] = (gender[x]+gender[y]+1)%2;
    return false;
}

int main(){
    int T = 0;
    int kase = 1;
    scanf("%d",&T);
    for(int t=0;t<T;t++){
        int n,m;
        scanf("%d %d",&n,&m);
        init();
        int flag =0;
        int a,b;
        for(int i=0;i<m;i++){
            scanf("%d %d",&a,&b);
            if(flag) continue;
            flag = merge(a,b);
        }
        printf("Scenario #%d:\n",kase++);
        if(flag==0) printf("No suspicious bugs found!\n\n");
        else    printf("Suspicious bugs found!\n\n");
    }
    return 0;
}

HDU1289 A Bug's Life (帶權並查集)