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

 #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. Ubuntu 16.04 LTS 配置 Jupyter notebook 为服务器

    原材料: Ubuntu 16.04 LTS 64bit 已经配置好 IPython 和 Jupyter (安装步骤可以参照:http://www.cnblogs.com/McKean/p/619497 ...

  2. 文件宝iOS/iPhone/iPad客户端简介

    App Store地址:https://itunes.apple.com/cn/app/id1023365565?mt=8 文件宝-装机必备的文件管家,专业的rar-zip 解压工具,局域网看片神器, ...

  3. python day- 5 字典(dic)的 增删改查 及 操作方法

    字典(dic) 1.定义及格式 用{ }大括号括起来的,由key:value 来保存数据的就是 字典(dic) eg:dic = {"及时雨" : "宋江" , ...

  4. Scaling with Microservices and Vertical Decomposition

    Scaling with Microservices and Vertical Decomposition – dev.otto.de https://dev.otto.de/2014/07/29/s ...

  5. Linux epoll 源码注释

    https://www.cnblogs.com/stonehat/p/8613505.html 这篇文章值得好好读,先留个记录,回头看. IO多路复用之epoll总结 - Anker's Blog - ...

  6. lc.exe 已退出 代码为 -1

    地址:http://jingyan.baidu.com/article/91f5db1bd0ace31c7f05e321.html

  7. 为了cider,尝试emacs的坑

    https://github.com/clojure-emacs/cider http://clojure-doc.org/articles/tutorials/emacs.html emacs通过b ...

  8. Uboot中start.S源码的指令级的详尽解析【转】

    本文转载自:http://www.crifan.com/files/doc/docbook/uboot_starts_analysis/release/html/uboot_starts_analys ...

  9. POJ3660 Cow Contest —— Floyd 传递闭包

    题目链接:http://poj.org/problem?id=3660 Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  10. SQL Server2005+、MySQL、Oracle 数据库字典生成工具

    之前找的数据库字典生成工具基本上都依赖于 Office Com 组件,在不安装 Office的情况下无法使用.怒,于是自己用C# 写了一个.     特征如下:         一.支持的数据库 MS ...