HDU 1829 A Bug's Life 【带权并查集/补集法/向量法】

时间:2023-03-09 01:05:08
HDU 1829 A Bug's Life 【带权并查集/补集法/向量法】
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. 

InputThe 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.OutputThe 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<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = +;
const int maxm = 1e6 + ;
const double PI = acos(-1.0);
const double eps = 1e-;
const int dx[] = {-,,,,,,-,-};
const int dy[] = {,,,-,,-,,-};
int dir[][] = {{,},{,-},{-,},{,}};
const int mon[] = {, , , , , , , , , , , , };
const int monn[] = {, , , , , , , , , , , , };
int t,n,m,d,x,y;
int fa[maxn*];
inline int read()
{
int sum=;
char ch=getchar();
while(ch>''||ch<'') ch=getchar();
while(ch>=''&&ch<='') sum=sum*+ch-,ch=getchar();
return sum;
}
int Find(int x)
{
return fa[x]==x ? x:fa[x]=Find(fa[x]);
}
void join(int x,int y)
{
int fx=Find(x);
int fy=Find(y);
if(fx!=fy) fa[fx]=fy;
}
bool same(int x,int y)
{
return Find(x)==Find(y);
}
int main()
{
int cas=;
scanf("%d",&t);
while(t--)
{
int f=;
n=read(),m=read();
int ans=;
for(int i=;i<=*n;i++) fa[i]=i;
for(int i=;i<=m;i++)
{
scanf("%d%d",&x,&y);
if(x>n||y>n){f=;continue;}
if(same(x,y) || same(x+n,y+n))
f=;
else
{
join(x+n,y);
join(x,y+n);
}
}
printf("Scenario #%d:\n",cas++);
if(f) printf("Suspicious bugs found!\n"); //1 3
else printf("No suspicious bugs found!\n");
cout<<endl;
}
return ;
} /*
【题意】
略 【类型】
带权并查集 【分析】
首先考虑如何用并查集解决问题:我们可以设置数组[1,2n],其中[a]表示男,[a+n]表示女
从头扫描每一对关系,将a和b+n,a+n和b合并(两人异性才可以合并在一个圈子)
合并之前检查a和b,a+n和b+n是否在同一集合,如果是,则为同性恋 【时间复杂度&&优化】 【trick】 【数据】
*/