码迷,mamicode.com
首页 > 其他好文 > 详细

HDU1289 A Bug's Life (带权并查集)

时间:2018-08-08 21:28:33      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:string   hint   arch   bugs   strong   思路   start   oid   OWIN   

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 (带权并查集)

标签:string   hint   arch   bugs   strong   思路   start   oid   OWIN   

原文地址:https://www.cnblogs.com/caomingpei/p/9445394.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!