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

 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = ;
int fa[maxn];
int Rank[maxn];
int group[maxn]; void init(int x){
fa[x] = x;
Rank[x] = ;
group[x] = ;
} int Find(int x){
if (x == fa[x])
return x;
else
return fa[x] = Find(fa[x]);
} void set_union(int x, int y){
int fx = Find(x);
int fy = Find(y);
if (Rank[fx] > Rank[fy]){
fa[fy] = fx;
}
else{
fa[fx] = fy;
if (Rank[fx] == Rank[fy])
Rank[fy]++;
}
} int main(){
int t;
int cnt = ;
scanf("%d", &t);
while (t--){
cnt++;
int n, m;
scanf("%d %d", &n, &m);
for (int i = ; i <= n; i++){
init(i);
}
bool ans = true;
while (m--){
int x;
int y;
scanf("%d %d", &x, &y);
if (ans == false)
continue;
if (Find(x) == Find(y)){
ans = false;
continue;
}
if (group[x] == )
group[x] = y;
else set_union(group[x], y);
if (group[y] == )
group[y] = x;
else
set_union(group[y], x); }
printf("Scenario #%d:\n", cnt);
if (!ans){
printf("Suspicious bugs found!\n");
}
else
printf("No suspicious bugs found!\n");
if (m != ){
printf("\n");
}
}
//system("pause");
return ;
}

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. iOS多线程编程(四)------ GCD(Grand Central Dispatch)

    一.简单介绍 是基于C语言开发的一套多线程开发机制.也是眼下苹果官方推荐的多线程开发方法.用起来也最简单.仅仅是它基于C语言开发,并不像NSOperation是面向对象的开发.而是全然面向过程的.假设 ...

  2. CSDN-markdown编辑器之从线上导入Markdown文件

      CSDN-markdown编辑器支持从线上导入Markdown文件的功能,假设你用其他支持Markdown的编辑器在网上写了博客文章或说明档,想公布到CSDN博客中,就能够使用本功能非常方便的完毕 ...

  3. __sizeof__()

    https://bugs.python.org/issue2898 https://bugs.python.org/file10353/footprint.patch Index: Python/sy ...

  4. Hibernate exception

    1.a different object with the same identifier value was already associated with the session. 错误原因:在h ...

  5. HTML canvas

    什么是 Canvas? HTML5 的 canvas 元素使用 JavaScript 在网页上绘制图像. 画布是一个矩形区域,您可以控制其每一像素. canvas 拥有多种绘制路径.矩形.圆形.字符以 ...

  6. CodeForces 559C Gerald and Gia (格路+容斥+DP)

    CodeForces 559C Gerald and Gia 大致题意:有一个 \(N\times M\) 的网格,其中有些格子是黑色的,现在需要求出从左上角到右下角不经过黑色格子的方案数(模 \(1 ...

  7. 使用cwRsync在Windows的目录之间增量同步文件

    http://www.qiansw.com/using-cwrsync-in-the-windows-directory-between-the-incremental-synchronization ...

  8. HDU3085 Nightmare Ⅱ —— 双向BFS + 曼哈顿距离

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Other ...

  9. Redis持久化(RDB和AOF)

    什么是Redis持久化 什么是Redis持久化,就是将内存数据保存到硬盘. Redis 持久化存储 (AOF 与 RDB 两种模式) RDB持久化 RDB 是以二进制文件,是在某个时间 点将数据写入一 ...

  10. 实现自定义xib和storyboard的加载,

    一:加载xib 1.分别创建xib,.h  .m文件继承自UIView. 在xib上绑定类名. 或者创建文件的时候直接勾选xib 2.在控制器中调用类方法 jyq52787网盘/ios/潭州学院/iO ...