把性别相同的虫子放在同一个集合,然后每读入一对虫子号,判断它们在不在同一集合,在则同性别,不在则继续

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. const int maxn = ;
  7. int fa[maxn];
  8. int Rank[maxn];
  9. int group[maxn];
  10.  
  11. void init(int x){
  12. fa[x] = x;
  13. Rank[x] = ;
  14. group[x] = ;
  15. }
  16.  
  17. int Find(int x){
  18. if (x == fa[x])
  19. return x;
  20. else
  21. return fa[x] = Find(fa[x]);
  22. }
  23.  
  24. void set_union(int x, int y){
  25. int fx = Find(x);
  26. int fy = Find(y);
  27. if (Rank[fx] > Rank[fy]){
  28. fa[fy] = fx;
  29. }
  30. else{
  31. fa[fx] = fy;
  32. if (Rank[fx] == Rank[fy])
  33. Rank[fy]++;
  34. }
  35. }
  36.  
  37. int main(){
  38. int t;
  39. int cnt = ;
  40. scanf("%d", &t);
  41. while (t--){
  42. cnt++;
  43. int n, m;
  44. scanf("%d %d", &n, &m);
  45. for (int i = ; i <= n; i++){
  46. init(i);
  47. }
  48. bool ans = true;
  49. while (m--){
  50. int x;
  51. int y;
  52. scanf("%d %d", &x, &y);
  53. if (ans == false)
  54. continue;
  55. if (Find(x) == Find(y)){
  56. ans = false;
  57. continue;
  58. }
  59. if (group[x] == )
  60. group[x] = y;
  61. else set_union(group[x], y);
  62. if (group[y] == )
  63. group[y] = x;
  64. else
  65. set_union(group[y], x);
  66.  
  67. }
  68. printf("Scenario #%d:\n", cnt);
  69. if (!ans){
  70. printf("Suspicious bugs found!\n");
  71. }
  72. else
  73. printf("No suspicious bugs found!\n");
  74. if (m != ){
  75. printf("\n");
  76. }
  77. }
  78. //system("pause");
  79. return ;
  80. }

hdu1829&&poj2492 A Bug's Life 基础种类并查集的更多相关文章

  1. hdu1829 A Bug's Life 基础种类并查集

    题目的大意可以理解为:A爱B,B爱C ……给出一系列爱恋的关系,推断有没有同性恋. 思路是把相同性别的归为一个集合(等价类),异性的异性为同性. #include<iostream> #i ...

  2. POJ-2492 A Bug's Life(种类并查集)

    http://poj.org/problem?id=2492 题意: 给出一个T代表几组数据,给出一个n一个m,代表人的编号由1~n,m条命令,每条命令由两个数值组成,代表这两个人性别不同,问所有命令 ...

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

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

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

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

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

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

  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. hdu 1182 A Bug's Life(简单种类并查集)

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

  8. poj2492 A Bug's Life【基础种类并查集】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4298148.html   ---by 墨染之樱花 题目链接:http://poj.org/pr ...

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

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

随机推荐

  1. HDU 1423 Greatest Common Increasing Subsequence(LICS入门,只要求出最长数)

    Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536 ...

  2. mysql有哪几种索引

    从数据结构角度 1.  B+树索引(O(log(n))) 2.  hash索引 3.  FULLTEXT索引 4.  R-Tree索引 从物理存储角度 1. 聚集索引 2.  非聚集索引 从逻辑角度 ...

  3. Android图表AChartEngine

    很多时候项目中我们需要对一些统计数据进行绘制表格,更多直观查看报表分析结果.基本有以下几种方法: 1:可以进行android api进行draw这样的话,效率比较低 2:使用开源绘表引擎,这样效率比较 ...

  4. PyOpenGL下GlutBitmapCharacter的替代

    虽然pyinstaller支持pyopengl,但是调用GLUT之后,在其它电脑上面运行就会出现错误,索性按照之前C#上面的办法,把字体数据和函数用python重写 fontData.py #! /u ...

  5. java8--面向对象 下(疯狂java讲义3) 复习笔记

    1.如果一个包装类和一个基本类型比较大小,或者是两个基本类型进行比较大小,直接用==就好: 如果是两个包装类进行比较大小,那么使用equals(),返回值是true,false,或者使用Xxx.com ...

  6. mysql优化---in型子查询,exists子查询,from 型子查询

    in型子查询引出的陷阱:(扫更少的行,不要临时表,不要文件排序就快) 题: 在ecshop商城表中,查询6号栏目的商品, (注,6号是一个大栏目) 最直观的: mysql); 误区: 给我们的感觉是, ...

  7. centos 网络连接查看

    安装iftop. 这个得先安装epel #yum install epel-replease -y #yum install iftop -y #iftop

  8. 解决ubuntu10.04不能上网

    1:命令行输入:lspci查看驱动,最后几行如果有ethernet controller:atheros communications ar8151 v1.0*的话,就说明驱动没有安装好, 2:下载地 ...

  9. I2S总线协议理解

    I2S总线 Inter IC Sound总线又称集成电路内置音频总线. I2S对数字音频设备之间的音频数据传输而制定的一种总线标准. 采用了沿独立的导线传输时钟与数据信号的设计,通过将数据和时钟信号分 ...

  10. POJ1860 Currency Exchange —— spfa求正环

    题目链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Tota ...