HDU1289 A Bug's Life (带权并查集)
Posted caomingpei
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU1289 A Bug's Life (带权并查集)相关的知识,希望对你有一定的参考价值。
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:
",kase++);
if(flag==0) printf("No suspicious bugs found!
");
else printf("Suspicious bugs found!
");
}
return 0;
}
以上是关于HDU1289 A Bug's Life (带权并查集)的主要内容,如果未能解决你的问题,请参考以下文章