A Bug's Life

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 8528    Accepted Submission(s): 2745

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
  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. Scenario #2:
  4. No suspicious bugs found!
  5. Hint
  6. Huge input,scanf is recommended.
  7.  

利用黑白染色,推断是否是二分图。

  1. #include"stdio.h"
  2. #include"string.h"
  3. #include"queue"
  4. using namespace std;
  5. #define N 2005
  6. #define M 1000005
  7. int n,m,t,head[N];
  8. int color[N],flag;
  9. struct node
  10. {
  11. int u,v,next;
  12. }map[2*M];
  13. void add(int u,int v)
  14. {
  15. map[t].u=u;
  16. map[t].v=v;
  17. map[t].next=head[u];
  18. head[u]=t++;
  19. map[t].u=v;
  20. map[t].v=u;
  21. map[t].next=head[v];
  22. head[v]=t++;
  23. }
  24. void find(int u)
  25. {
  26. int i,v;
  27. for(i=head[u];i!=-1;i=map[i].next)
  28. {
  29. v=map[i].v;
  30. if(color[v]==-1)
  31. {
  32. color[v]=color[u]^1;
  33. find(v);
  34. }
  35. else if(color[v]==color[u])
  36. {
  37. flag=1;
  38. return ;
  39. }
  40. }
  41. return ;
  42. }
  43. int main()
  44. {
  45. int i,T,u,v,cnt=1;
  46. scanf("%d",&T);
  47. while(T--)
  48. {
  49. scanf("%d%d",&n,&m);
  50. t=0;
  51. memset(head,-1,sizeof(head));
  52. while(m--)
  53. {
  54. scanf("%d%d",&u,&v);
  55. add(u,v);
  56. }
  57. memset(color,-1,sizeof(color));
  58. flag=0;
  59. for(i=1;i<=n;i++)
  60. {
  61. if(color[i]==-1)
  62. {
  63. color[i]=0;
  64. find(i);
  65. if(flag)
  66. break;
  67. }
  68. }
  69. printf("Scenario #%d:\n",cnt++);
  70. if(flag)
  71. printf("Suspicious bugs found!\n");
  72. else
  73. printf("No suspicious bugs found!\n");
  74. puts("");
  75. }
  76. return 0;
  77. }

带权并查集:

  1. #include"stdio.h"
  2. #include"string.h"
  3. #include"queue"
  4. #include"vector"
  5. #include"stack"
  6. #include"algorithm"
  7. using namespace std;
  8. #define N 2005
  9. #define min(a,b) (a<b?a:b)
  10. int pre[N],gen[N];
  11. int find(int k)
  12. {
  13. if(k==pre[k])
  14. return k;
  15. int t=find(pre[k]); //不能马上更新父节点与根节点同样
  16. gen[k]=gen[k]^gen[pre[k]];//由于该节点的性别和它的父节点相反
  17. return pre[k]=t; //确定该点性别之后才干把该点父节点更新为根节点
  18. }
  19. int Union(int x,int y)
  20. {
  21. int a,b;
  22. a=find(x);
  23. b=find(y);
  24. if(a==b) //x,y的根节点同样
  25. {
  26. if(gen[x]==gen[y]) //推断他们是否同性别
  27. return 1;
  28. return 0;
  29. }
  30. pre[a]=b;
  31. gen[a]=(gen[x]+gen[y]+1)&1;
  32. return 0;
  33. }
  34. int main()
  35. {
  36. int i,u,v,n,cnt=1,T,m;
  37. scanf("%d",&T);
  38. while(T--)
  39. {
  40. scanf("%d%d",&n,&m);
  41. for(i=0;i<=n;i++)
  42. pre[i]=i;
  43. memset(gen,0,sizeof(gen));
  44. int flag=0;
  45. while(m--)
  46. {
  47. scanf("%d%d",&u,&v);
  48. if(flag)
  49. continue;
  50. flag=Union(u,v);
  51. }
  52. printf("Scenario #%d:\n",cnt++);
  53. if(flag)
  54. printf("Suspicious bugs found!\n");
  55. else
  56. printf("No suspicious bugs found!\n");
  57. puts("");
  58. }
  59. return 0;
  60. }

hdu 1829 &amp;poj 2492 A Bug&#39;s Life(推断二分图、带权并查集)的更多相关文章

  1. POJ 2912 Rochambeau(难,好题,枚举+带权并查集)

    下面的是从该网站上copy过来的,稍微改了一点,给出链接:http://hi.baidu.com/nondes/item/26dd0f1a02b1e0ef5f53b1c7 题意:有N个人玩剪刀石头布, ...

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

    A Bug's Life POJ - 2492 Background Professor Hopper is researching the sexual behavior of a rare spe ...

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

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

  4. hdu 1829-A Bug's LIfe(简单带权并查集)

    题意:Bug有两种性别,异性之间才交往, 让你根据数据判断是否存在同性恋,输入有 t 组数据,每组数据给出bug数量n, 和关系数m, 以下m行给出相交往的一对Bug编号 a, b.只需要判断有没有, ...

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

    题目链接 http://poj.org/problem?id=2492 题意 虫子有两种性别,有n只虫子,编号1~n,输入m组数据,每组数据包含a.b两只虫子,表示a.b为不同性别的虫子,根据输入的m ...

  6. 【poj 1984】&【bzoj 3362】Navigation Nightmare(图论--带权并查集)

    题意:平面上给出N个点,知道M个关于点X在点Y的正东/西/南/北方向的距离.问在刚给出一定关系之后其中2点的曼哈顿距离((x1,y1)与(x2,y2):l x1-x2 l+l y1-y2 l),未知则 ...

  7. 【poj 1988】Cube Stacking(图论--带权并查集)

    题意:有N个方块,M个操作{"C x":查询方块x上的方块数:"M x y":移动方块x所在的整个方块堆到方块y所在的整个方块堆之上}.输出相应的答案. 解法: ...

  8. POJ 1733 Parity game(离散化+带权并查集)

    离散化+带权并查集 题意:长度为n的0和1组成的字符串,然后问第L和R位置之间有奇数个1还是偶数个1. 根据这些回答, 判断第几个是错误(和之前有矛盾)的. 思路:此题同HDU 3038 差不多,询问 ...

  9. hdu 5441 Travel 离线带权并查集

    Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...

随机推荐

  1. 初步接触CERNVM

    初步接触的来源是对ROOT数据分析工具的搜索,看到一个叫做Life as a Physicist的国外博客.知道了这个包含容器分发的软件,跟重要的是,这个欧洲核子中心开发的平台,对于我等科研人员是一大 ...

  2. Xamarin XAML语言教程ContentView视图作为自定义视图的父类

    Xamarin XAML语言教程ContentView视图作为自定义视图的父类 自定义视图的父类:ContentView视图可以作为自定义视图的父类. [示例14-2]以下将自定义一个颜色视图.具体的 ...

  3. 【动态规划】【记忆化搜索】【搜索】CODEVS 1262 不要把球传我 2012年CCC加拿大高中生信息学奥赛

    可以暴力递归求解,应该不会TLE,但是我们考虑记忆化优化. 设f(i,j)表示第i个数为j时的方案数. f(i,j)=f(1,j-1)+f(2,j-1)+……+f(i-1,j-1) (4>=j& ...

  4. (转)[Unity3D]关于Assets资源目录结构管理

    分享个我们项目常用的目录结构,微调过很多次,最终到了这个版本.个人认为这种管理资源方式是不错的.欢迎探讨各个细节~ 更新于2013.5.30   Asserts   --Editor 自写的灵活方便插 ...

  5. Delphi 7生成XML

    文件格式为: Day 制1課 U12 ASSY01 Wrist 1009 0 2018/05/18 09:35:59 Day 制1課 U12 ASSY02 Wrist 1010 0 2018/05/1 ...

  6. ASP.NET Core 1.0基础之日志

    过年出去玩了一圈,回来继续翻译.前两天偷懒没有翻译,只是转了两篇C# 7计划中的新features,大家还是很支持的.现在继续完善这个系列. 来源https://docs.asp.net/en/lat ...

  7. ASP.NET Core 1.0基础之诊断

    来源https://docs.asp.net/en/latest/fundamentals/diagnostics.html ASP.NET Core 1.0包含了一些新的特性来辅助诊断问题.可以在S ...

  8. Listener监听器之HttpSessionListener

    编写一个OnlineUserListener. package anni; import java.util.List; import javax.servlet.ServletContext; im ...

  9. represent states with objects

    1. The behavior of objects in the real world is more complex than simply being in one state at a tim ...

  10. 【codeforces #282(div 1)】AB题解

    A. Treasure time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...