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

  1. 2
  2. 3 3
  3. 1 2
  4. 2 3
  5. 1 3
  6. 4 2
  7. 1 2
  8. 3 4

Sample Output

  1. Scenario #1:
  2. Suspicious bugs found!
  3.  
  4. Scenario #2:
  5. No suspicious bugs found!

Hint

  1. Huge input,scanf is recommended.
  1. #include<cstdio>
  2. #include<string>
  3. #include<cstdlib>
  4. #include<cmath>
  5. #include<iostream>
  6. #include<cstring>
  7. #include<set>
  8. #include<queue>
  9. #include<algorithm>
  10. #include<vector>
  11. #include<map>
  12. #include<cctype>
  13. #include<stack>
  14. #include<sstream>
  15. #include<list>
  16. #include<assert.h>
  17. #include<bitset>
  18. #include<numeric>
  19. #define debug() puts("++++")
  20. #define gcd(a,b) __gcd(a,b)
  21. #define lson l,m,rt<<1
  22. #define rson m+1,r,rt<<1|1
  23. #define fi first
  24. #define se second
  25. #define pb push_back
  26. #define sqr(x) ((x)*(x))
  27. #define ms(a,b) memset(a,b,sizeof(a))
  28. #define sz size()
  29. #define be begin()
  30. #define pu push_up
  31. #define pd push_down
  32. #define cl clear()
  33. #define lowbit(x) -x&x
  34. #define all 1,n,1
  35. #define rep(i,x,n) for(int i=(x); i<(n); i++)
  36. #define in freopen("in.in","r",stdin)
  37. #define out freopen("out.out","w",stdout)
  38. using namespace std;
  39. typedef long long LL;
  40. typedef unsigned long long ULL;
  41. typedef pair<int,int> P;
  42. const int INF = 0x3f3f3f3f;
  43. const LL LNF = 1e18;
  44. const int maxn = +;
  45. const int maxm = 1e6 + ;
  46. const double PI = acos(-1.0);
  47. const double eps = 1e-;
  48. const int dx[] = {-,,,,,,-,-};
  49. const int dy[] = {,,,-,,-,,-};
  50. int dir[][] = {{,},{,-},{-,},{,}};
  51. const int mon[] = {, , , , , , , , , , , , };
  52. const int monn[] = {, , , , , , , , , , , , };
  53. int t,n,m,d,x,y;
  54. int fa[maxn*];
  55. inline int read()
  56. {
  57. int sum=;
  58. char ch=getchar();
  59. while(ch>''||ch<'') ch=getchar();
  60. while(ch>=''&&ch<='') sum=sum*+ch-,ch=getchar();
  61. return sum;
  62. }
  63. int Find(int x)
  64. {
  65. return fa[x]==x ? x:fa[x]=Find(fa[x]);
  66. }
  67. void join(int x,int y)
  68. {
  69. int fx=Find(x);
  70. int fy=Find(y);
  71. if(fx!=fy) fa[fx]=fy;
  72. }
  73. bool same(int x,int y)
  74. {
  75. return Find(x)==Find(y);
  76. }
  77. int main()
  78. {
  79. int cas=;
  80. scanf("%d",&t);
  81. while(t--)
  82. {
  83. int f=;
  84. n=read(),m=read();
  85. int ans=;
  86. for(int i=;i<=*n;i++) fa[i]=i;
  87. for(int i=;i<=m;i++)
  88. {
  89. scanf("%d%d",&x,&y);
  90. if(x>n||y>n){f=;continue;}
  91. if(same(x,y) || same(x+n,y+n))
  92. f=;
  93. else
  94. {
  95. join(x+n,y);
  96. join(x,y+n);
  97. }
  98. }
  99. printf("Scenario #%d:\n",cas++);
  100. if(f) printf("Suspicious bugs found!\n"); //1 3
  101. else printf("No suspicious bugs found!\n");
  102. cout<<endl;
  103. }
  104. return ;
  105. }
  106.  
  107. /*
  108. 【题意】
  109.  
  110. 【类型】
  111. 带权并查集
  112.  
  113. 【分析】
  114. 首先考虑如何用并查集解决问题:我们可以设置数组[1,2n],其中[a]表示男,[a+n]表示女
  115. 从头扫描每一对关系,将a和b+n,a+n和b合并(两人异性才可以合并在一个圈子)
  116. 合并之前检查a和b,a+n和b+n是否在同一集合,如果是,则为同性恋
  117.  
  118. 【时间复杂度&&优化】
  119.  
  120. 【trick】
  121.  
  122. 【数据】
  123. */

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. Test Index

    top1 top11 top2 top1 top11 top2

  2. Item 6 消除过期的对象引用

    过期对象引用没有清理掉,会导致内存泄漏.对于没有用到的对象引用,可以置空,这是一种做法.而最好的做法是,把保存对象引用的变量清理掉,多用局部变量.   什么是内存泄漏? 在Java中,对象的内存空间回 ...

  3. UIView显示时遮挡导航栏的方法

    [self.navigationController.view:addSubview];

  4. quick-cocos2dx lua中读取 加密 csv表

    我非常想把一些非必需的信息以CSV表的格式保存到客户端,以减少和服务器的通讯,降低压力.于是写了这么一个. 但因为大家觉得这样的话,需要每次登陆时来检测同步这些数据,会减慢登陆速度,于是没有用到. 我 ...

  5. SCU 1029 Humble Numbers (打表处理)

    题目链接 题意:素因子中只有2 3 5 7的数称为谦逊的数,1也是谦逊的数,题目中已经给出了前20个谦逊的数.给定数字n,按格式输出第n个谦逊的数. 题解:打表即可,注意打表的技巧就行了. ps:这道 ...

  6. bzoj 1483 链表启发式合并

    首先我们可以比较容易的在n的时间内算出来开始的答案,我们维护一些链表,分别表示不同的颜色,那么我们在计算答案的时候,只需要扫一遍所有的链表,判断链表相邻两项是否在序列中相邻,不相邻的话肯定在这其中的一 ...

  7. bzoj 2733 平衡树启发式合并

    首先对于一个连通块中,询问我们可以直接用平衡树来求出排名,那么我们可以用并查集来维护各个块中的连通情况,对于合并两个平衡树,我们可以暴力的将size小的平衡树中的所有节点删掉,然后加入大的平衡树中,因 ...

  8. ...args剩余参数用法

      剩余参数语法允许我们将一个不定数量的参数表示为一个数组. function sum(...theArgs) { return theArgs.reduce((previous, current) ...

  9. 【tomcat】手动部署动态JavaWeb项目到tomcat

    1.通过修改server.xml进行配置 1.查看项目的目录结构: tomcat运行时加载WebConmtent目录

  10. sqlmap参数说明

    --delay 设置每隔几秒测试一次注入 --safe-url 设置sqlmap要访问的正常url --safe-freq 设置每测试多少条注入语句后才去访问safe-url --code 设置能正常 ...