传送门:

http://acm.hdu.edu.cn/showproblem.php?pid=1829

A Bug's Life

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

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.

 
Source
 
Recommend
linle
 
题目意思:
Bug有两种性别,异性之间才交往, 让你根据数据判断是否存在同性恋,输入有 t 组数据,每组数据给出bug数量n, 和关系数m, 以下m行给出相交往的一对Bug编号 a, b。只需要判断有没有,按题目要求输出
分析:
两个种类(两性)
种类并查集
判断存在同性恋的标准:
根节点相同的且性别相同的则是同性恋。
 
code:
  1. #include <iostream>
  2. #include<algorithm>
  3. #include <cstdio>
  4. #include<cstring>
  5. #include<math.h>
  6. #include<memory>
  7. using namespace std;
  8. typedef long long LL;
  9. #define max_v 2005
  10. int pa[max_v];//前驱
  11. int re[max_v];//性别
  12. int flag;
  13. void make_set(int x)
  14. {
  15. pa[x]=x;
  16. re[x]=;
  17. }
  18. int find_set(int x)
  19. {
  20. if(x==pa[x])
  21. return pa[x];
  22. int t=find_set(pa[x]);
  23. re[x]=(re[pa[x]]+re[x])%;
  24. pa[x]=t;
  25. return pa[x];
  26. }
  27. void union_set(int x,int y)
  28. {
  29. int a=find_set(x);
  30. int b=find_set(y);
  31. if(a==b)
  32. {
  33. if(re[x]==re[y])
  34. flag=;//根节点相同的且性别相同的则是同性恋。
  35. return ;
  36. }
  37. pa[a]=b;
  38. re[a]=(re[x]-re[y]+)%;//保证 0 1
  39. }
  40. int main()
  41. {
  42. int t,j=;
  43. scanf("%d",&t);
  44. while(t--)
  45. {
  46. int n,m;
  47. scanf("%d %d",&n,&m);
  48. flag=;
  49. for(int i=;i<=n;i++)
  50. make_set(i);
  51. for(int i=;i<m;i++)
  52. {
  53. int a,b;
  54. scanf("%d %d",&a,&b);
  55. if(flag==)
  56. continue;
  57. union_set(a,b);
  58. }
  59. printf("Scenario #%d:\n",j++);
  60. if(flag==)
  61. printf("Suspicious bugs found!\n");
  62. else
  63. printf("No suspicious bugs found!\n");
  64. printf("\n");
  65. }
  66. return ;
  67. }
 

HDU 1829 A Bug's Life (种类并查集)的更多相关文章

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

    思路:见代码吧. #include <stdio.h> #include <string.h> #include <set> #include <vector ...

  2. hdoj 1829 A bug's life 种类并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 并查集的一个应用,就是检测是否存在矛盾,就是两个不该相交的集合有了交集.本题就是这样,一种虫子有 ...

  3. 【POJ】2492 A bug's life ——种类并查集

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

  4. POJ2492 A Bug's Life —— 种类并查集

    题目链接:http://poj.org/problem?id=2492 A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Su ...

  5. hdu1829A Bug's Life(种类并查集)

    传送门 关键在于到根节点的距离,如果两个点到根节点的距离相等,那么他们性别肯定就一样(因为前面如果没有特殊情况,两个点就是一男一女的).一旦遇到性别一样的,就说明找到了可疑的 #include< ...

  6. 【进阶——种类并查集】hdu 1829 A Bug's Life (基础种类并查集)TUD Programming Contest 2005, Darmstadt, Germany

    先说说种类并查集吧. 种类并查集是并查集的一种.但是,种类并查集中的数据是分若干类的.具体属于哪一类,有多少类,都要视具体情况而定.当然属于哪一类,要再开一个数组来储存.所以,种类并查集一般有两个数组 ...

  7. hdu 1182 A Bug's Life(简单种类并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 题意:就是给你m条关系a与b有性关系,问这些关系中是否有同性恋 这是一道简单的种类并查集,而且也 ...

  8. A Bug's Life(种类并查集)(也是可以用dfs做)

    http://acm.hdu.edu.cn/showproblem.php?pid=1829   A Bug's Life Time Limit:5000MS     Memory Limit:327 ...

  9. POJ2492:A Bug's Life(种类并查集)

    A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 45757   Accepted: 14757 题 ...

随机推荐

  1. js中常用的算法排序

    在工作中都会经常用到的一些基础算法,可以很快解决问题.这些都是在工作中总结的,希望可以帮助到大家. 一.数组乱序 arr.sort(function randomsort(a, b) { return ...

  2. Angular进阶教程三

    7 总结 angular上手比较难,初学者(特别是习惯了使用JQuery的人)可能不太适应其语法以及思想.随着对ng探索的一步步深入,也确实感觉到了这一点,尤其是框架内部的某些执行机制. 7.1页面效 ...

  3. windows10(本机)与VirtualBox中CentOS7(虚拟机)互相访问总结

    先把我这里的环境说下: 本机(windows10),发布了一个tomcat服务:http://192.168.0.106:8080/axis/services/VPMService?wsdl 如下图: ...

  4. 解决ArcMap启动时只停留在初始化界面的方法

    方法1 修改环境变量TEMP和TMP为C:\Temp 重启ArcMap. 方法2 关闭系统进程Print Spooler. 打开C:\WINDOWS\system32\spool\PRINTERS,删 ...

  5. Grunt入门学习之(3) -- Gruntfile具体示例

    经过前面的学习,将测试的Gruntfile整合在一起! /** * Created by Administrator on 2017/6/22. */ module.exports = functio ...

  6. Evernote Markdown Sublime实现

    版权声明: 欢迎转载,但请保留文章原始出处 作者:GavinCT 出处:http://www.cnblogs.com/ct2011/p/3996164.html Evernote无法实现markdow ...

  7. Python 爬虫练手项目—酒店信息爬取

    from bs4 import BeautifulSoup import requests import time import re url = 'http://search.qyer.com/ho ...

  8. ASP.NET错误处理的方式(一)

    对Web应用程序来说,发生不可预知的错误和异常在所难免,我们必须为Web程序提供错误处理机制.当错误发生时,我们必须做好两件事情:一是将错误信息记录日志,发邮件通知网站维护人员,方便技术人员对错误进行 ...

  9. 在 Windows Server Container 中运行 Azure Storage Emulator(一):能否监听自定义地址?

    我要做什么? 改 ASE 的监听地址.对于有强迫症的我来说,ASE 默认监听的是 127.0.0.1:10000-10002,这让我无法接受,所以我要将它改成域名 + 80 端口的方式: 放到容器中. ...

  10. 使用TryUpdateModel进行数据更新

    在控制器中可以使用TryUpdateModel或者UpdateModel方法来对指定的数据Model进行更新,如图所示的更新操作. POST请求数据如下所示 使用如下代码就可以对指定的字段进行更新 使 ...