A Bug's Life

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11981    Accepted Submission(s): 3901

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.

 
并查集题目。
记录一下当前结点到根结点的距离。 当再次找进行”并“操作时,判断属于同一根节点的两个节点到根节点的距离,同时为奇数或偶数,那么久存在矛盾。
  

  1. /* ***********************************************
  2. Author :pk28
  3. Created Time :2015/8/15 9:54:22
  4. File Name :4.cpp
  5. ************************************************ */
  6. #include <iostream>
  7. #include <cstring>
  8. #include <cstdlib>
  9. #include <stdio.h>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <queue>
  13. #include <set>
  14. #include <map>
  15. #include <string>
  16. #include <math.h>
  17. #include <stdlib.h>
  18. #include <iomanip>
  19. #include <list>
  20. #include <deque>
  21. #include <stack>
  22. #define ull unsigned long long
  23. #define ll long long
  24. #define mod 90001
  25. #define INF 0x3f3f3f3f
  26. #define maxn 3000+10
  27. #define cle(a) memset(a,0,sizeof(a))
  28. const ull inf = 1LL << ;
  29. const double eps=1e-;
  30. using namespace std;
  31.  
  32. bool cmp(int a,int b){
  33. return a>b;
  34. }
  35. int fa[maxn];
  36. int d[maxn];
  37. int n,m,mark;
  38. void init(){
  39. for(int i=;i<=n+;i++){
  40. d[i]=;
  41. fa[i]=i;
  42. }
  43. mark=;
  44. }
  45. int findfa(int x){
  46. if(x==fa[x])return x;
  47. else{
  48. int root=findfa(fa[x]);
  49. d[x]+=d[fa[x]];//记录到根节点的距离
  50. fa[x]=root;
  51. return fa[x];
  52. }
  53. }
  54. void Union(int a,int b){
  55. int x=findfa(a);
  56. int y=findfa(b);
  57. if(x==y){
  58. if((d[a]&)==(d[b]&))mark=;
  59. return ;
  60. }
  61. else{
  62. fa[x]=y;
  63. d[x]=(1+d[b]-d[a]);//向量
  64. }
  65. }
  66. int main()
  67. {
  68. #ifndef ONLINE_JUDGE
  69. freopen("in.txt","r",stdin);
  70. #endif
  71. //freopen("out.txt","w",stdout);
  72. int T;
  73. cin>>T;
  74. int a,b;
  75. int cnt=;
  76. while(T--){
  77. scanf("%d%d",&n,&m);
  78. init();
  79. for(int i=;i<m;i++){
  80. scanf("%d %d",&a,&b);
  81. if(mark)continue;
  82. Union(a,b);
  83. }
  84. printf("Scenario #%d:\n",cnt++);
  85. if(mark)printf("Suspicious bugs found!\n");
  86. else printf("No suspicious bugs found!\n");
  87. printf("\n");
  88. }
  89. return ;
  90. }

HDU 1829/POJ 2492 A Bug's Life的更多相关文章

  1. hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them

    http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...

  2. hdu 1829 &amp;poj 2492 A Bug&#39;s Life(推断二分图、带权并查集)

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

  3. POJ 2492 A Bug's Life(并查集)

    http://poj.org/problem?id=2492 题意 :就是给你n条虫子,m对关系,每一对关系的双方都是异性的,让你找出有没有是同性恋的. 思路 :这个题跟POJ1703其实差不多,也是 ...

  4. (简单) POJ 2492 A Bug's Life,二分染色。

    Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs ...

  5. POJ 2492 A Bug's Life(带权并查集)

    题目链接:http://poj.org/problem?id=2492 题目大意:有n只虫子,m对关系,m行每行有x y两个编号的虫子,告诉你每对x和y都为异性,先说的是对的,如果后面给出关系与前面的 ...

  6. POJ 2492 A Bug's Life

    传送门:A Bug's Life Description Background Professor Hopper is researching the sexual behavior of a rar ...

  7. POJ 2492 A Bug's Life (并查集)

    A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 30130   Accepted: 9869 De ...

  8. POJ 2492 A Bug's Life (并查集)

    Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...

  9. POJ 2492 A Bug's Life【并查集高级应用+类似食物链】

    Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...

随机推荐

  1. 重复造轮子之RSA算法(一) 大素数生成

    出于无聊, 打算从头实现一遍RSA算法 第一步, 大素数生成 Java的BigInteger里, 有个现成的方法 public static BigInteger probablePrime(int ...

  2. R语言实战读书笔记(十三)广义线性模型

    # 婚外情数据集 data(Affairs, package = "AER") summary(Affairs) table(Affairs$affairs) # 用二值变量,是或 ...

  3. Codeforces 375D Tree and Queries(DFS序+莫队+树状数组)

    题目链接  Tree and Queries 题目大意  给出一棵树和每个节点的颜色.每次询问$vj, kj$ 你需要回答在以$vj$为根的子树中满足条件的的颜色数目, 条件:具有该颜色的节点数量至少 ...

  4. 如何轻松的把图片导入execl表格中

    在项目中有时候会遇到往数据库中导数据的时候,往往需要把图片也一起导入execl表格中,那怎么才能把图片一块导入至execl中呢?那么今天我们就来看看怎么实现吧! 如何实现?今天我们就来用jxl和poi ...

  5. kafka技术分享01--------why we study kafka?

    kafka技术分享01--------why we study kafka? ​ 作为一名大数据工程师,我们所面对的大多数是数据密集型的应用,而非计算密集型的应用.对于数据密集型的应用,如何解决数据激 ...

  6. SqlServer 并发事务:死锁跟踪(三)6种跟踪死锁的方法总结 大神

    http://blog.csdn.net/kk185800961/article/details/42504857

  7. pt-pmp :pt toolkit

    http://www.cnblogs.com/ivictor/p/6012183.html

  8. DELPHI跨平台编译开关

    DELPHI跨平台编译开关 DELPHI 现在是跨平台的开发工具,已经不仅仅针对WINDOWS OS. 跨平台的时候,一些WINDOWS特有的API或语法是不能用的,必须使用跨平台的新语法,要用编译开 ...

  9. jenkins执行单元测试,会产生大量临时文件,要及时删除,不然会把inode耗尽

    jenkins的build命令:clean test -U findbugs:findbugs pmd:pmd sonar:sonar -Djava.io.tmpdir=/tmp/ -Dsonar.p ...

  10. DICOM医学图像处理:Deconstructed PACS之Orthanc,Modification & Anonymization

    背景: 上篇博文为引子,介绍了一款神奇的开源PACS系统——Orthanc.本篇开始解读官方Cookbook中的相关内容,对于简单的浏览.访问和上传请阅读前篇博文.在常规的PACS系统中还未出现对于D ...