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】 【数据】
*/

HDU 1829 A Bug's Life 【带权并查集/补集法/向量法】的更多相关文章

  1. POJ2492 A Bug's Life 带权并查集

    分析:所谓带权并查集,就是比朴素的并查集多了一个数组,记录一些东西,例如到根的距离,或者和根的关系等 这个题,权数组为relation 代表的关系  1 和父节点不同性别,0,和父节点同性别 并查集一 ...

  2. HDU 5176 The Experience of Love 带权并查集

    The Experience of Love Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/O ...

  3. POJ 2492 A Bug's Life 带权并查集

    题意: 思路: mod2 意义下的带权并查集 如果两只虫子是异性恋,它们的距离应该是1. 如果两只虫子相恋且距离为零,则它们是同性恋. (出题人好猥琐啊) 注意: 不能输入一半就break出来.... ...

  4. POJ 1182 食物链 【带权并查集/补集法】

    动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种.有人用两种说 ...

  5. hdu 1829 A Bug's Life(分组并查集(偏移量))

    A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  6. HDU 1829 A Bug's Life (种类并查集)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1829 A Bug's Life Time Limit: 15000/5000 MS (Java/Oth ...

  7. Travel(HDU 5441 2015长春区域赛 带权并查集)

    Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Su ...

  8. poj2492A Bug's Life——带权并查集

    题目:http://poj.org/problem?id=2492 所有元素加入同一个并查集中,通过其偏移量%2将其分类为同性与异性,据此判断事件. 代码如下: #include<iostrea ...

  9. POJ 2492 A Bug's Life (带权并查集 && 向量偏移)

    题意 : 给你 n 只虫且性别只有公母, 接下来给出 m 个关系, 这 m 个关系中都是代表这两只虫能够交配, 就是默认异性, 问你在给出的关系中有没有与异性交配这一事实相反的, 即同性之间给出了交配 ...

随机推荐

  1. (转)史上最好的Python线程指南

    来自AstalWind的好文,彻底认识python线程 http://www.cnblogs.com/huxi/archive/2010/06/26/1765808.html . . . . .

  2. 南阳ACM 题目811:变态最大值 Java版

    变态最大值 时间限制:1000 ms  |  内存限制:65535 KB 难度:1 描述 Yougth讲课的时候考察了一下求三个数最大值这个问题,没想到大家掌握的这么烂,幸好在他的帮助下大家算是解决了 ...

  3. UVA 1640 The Counting Problem

    https://vjudge.net/problem/UVA-1640 题意:统计区间[l,r]中0——9的出现次数 数位DP 注意删除前导0 #include<cmath> #inclu ...

  4. mysql 可视化界面操作指令

    1.让自增长从新开始 ALTER TABLE users auto_increment =1;//让表中的自增长从新从0开始 2.条件查询 SELECT name from  users WHERE ...

  5. 【BZOJ4870】组合数问题 [矩阵乘法][DP]

    组合数问题 Time Limit: 10 Sec  Memory Limit: 512 MB[Submit][Status][Discuss] Description Input 第一行有四个整数 n ...

  6. JS之document例题讲解2

    例题三.图片轮播 <body> <div style="width:1000px; height:250px; margin-top:30px"> < ...

  7. 12.24笔记(关于//UIDynamic演练//多对象的附加行为//UIDynamic简单演练//UIDynamic//(CoreText框架)NSAttributedString)

          12.24笔记1.UIDynamic注意点:演示代码:上面中设置视图旋转的时候,需要注意设置M_PI_4时,视图两边保持平衡状态,达不到仿真效果.需要偏移下角度.2.吸附行为3.推动行为初 ...

  8. 分类算法:决策树(C4.5)(转)

    C4.5是机器学习算法中的另一个分类决策树算法,它是基于ID3算法进行改进后的一种重要算法,相比于ID3算法,改进有如下几个要点: 1)用信息增益率来选择属性.ID3选择属性用的是子树的信息增益,这里 ...

  9. 【转】ps命令详解

    原文地址:http://apps.hi.baidu.com/share/detail/32573968 有 时候系统管理员可能只关心现在系统中运行着哪些程序,而不想知道有哪些进程在运行.由于一个应用程 ...

  10. js/jq 键盘上下左右回车按键

    js判断上下左右回车按键: document.onkeydown=function(e){ e=window.event||e; switch(e.keyCode){ case 37: //左键 co ...