hdu1829&&poj2492 A Bug's Life 基础种类并查集
把性别相同的虫子放在同一个集合,然后每读入一对虫子号,判断它们在不在同一集合,在则同性别,不在则继续
#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 基础种类并查集的更多相关文章
- hdu1829 A Bug's Life 基础种类并查集
题目的大意可以理解为:A爱B,B爱C ……给出一系列爱恋的关系,推断有没有同性恋. 思路是把相同性别的归为一个集合(等价类),异性的异性为同性. #include<iostream> #i ...
- POJ-2492 A Bug's Life(种类并查集)
http://poj.org/problem?id=2492 题意: 给出一个T代表几组数据,给出一个n一个m,代表人的编号由1~n,m条命令,每条命令由两个数值组成,代表这两个人性别不同,问所有命令 ...
- 【进阶——种类并查集】hdu 1829 A Bug's Life (基础种类并查集)TUD Programming Contest 2005, Darmstadt, Germany
先说说种类并查集吧. 种类并查集是并查集的一种.但是,种类并查集中的数据是分若干类的.具体属于哪一类,有多少类,都要视具体情况而定.当然属于哪一类,要再开一个数组来储存.所以,种类并查集一般有两个数组 ...
- POJ2492:A Bug's Life(种类并查集)
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 45757 Accepted: 14757 题 ...
- A Bug's Life(种类并查集)(也是可以用dfs做)
http://acm.hdu.edu.cn/showproblem.php?pid=1829 A Bug's Life Time Limit:5000MS Memory Limit:327 ...
- 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 ...
- hdu 1182 A Bug's Life(简单种类并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 题意:就是给你m条关系a与b有性关系,问这些关系中是否有同性恋 这是一道简单的种类并查集,而且也 ...
- poj2492 A Bug's Life【基础种类并查集】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4298148.html ---by 墨染之樱花 题目链接:http://poj.org/pr ...
- POJ2492 A Bug's Life 带权并查集
分析:所谓带权并查集,就是比朴素的并查集多了一个数组,记录一些东西,例如到根的距离,或者和根的关系等 这个题,权数组为relation 代表的关系 1 和父节点不同性别,0,和父节点同性别 并查集一 ...
随机推荐
- TinyXML:属性
TiXmlAttribute: 代表XML中的属性,TiXmlAttribute中定义了一系列对属性的操作 TiXmlAttribute的友元类: friend class TiXmlAttribut ...
- zoj1232Adventure of Super Mario(图上dp)
题目连接: 啊哈哈.点我点我 思路: 这个题目是一个图上dp问题.先floyd预处理出图上全部点的最短路,可是在floyd的时候,把可以用神器的地方预处理出来,也就是转折点地方不能为城堡..预处理完成 ...
- JfreeChart折线图 CSDN-李鹏飞
今天公司里分配给我的工作是JfreeChart折线图本人之前也没接触过如今让我们大家一起完毕! 在这个公司,用到了太多的JfreeChart,今天就对折线图作一个总结,希望对大家有点帮助,我这里直接是 ...
- mac系统不同java版本切换
#确认jdk版本 /usr/libexec/java_home #会得到下面信息(不同版本显示不一样) /Library/Java/JavaVirtualMachines/jdk1.7.0_60.jd ...
- 多媒体开发之---h264 取流解码分析
1. nalu_unit_type = *((unsigned char *)pEmptyBuf->bufVirtAddr+4); nalu_unit_type = nalu_unit_type ...
- URAL 1731. Dill(数学啊 )
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1731 1731. Dill Time limit: 0.5 second Memory ...
- 从小姐姐博客那里看到的流光文字(CSS3 animate)
对于流光文字,大家并不陌生,毕竟我们都经历过非主流的时代.你们卟懂绯紸流!色彩缤纷的QQ空间...... 还记得那些炫酷的签名档,或者那些炫酷的动态头像.不过大家对于流光文字的印象还是图片.那么在网页 ...
- Spring Boot 访问静态资源
方法1一: 在resources目录下建立static的目录,将静态资源放到此处,可以直接访问 访问:127.0.0.1:9010/img/123.png
- Hadoop MapReduce两种架构 以及 YARN
一.MRv1 Master - Slave 模式 存在JobTracker单点失败的问题,在YARN得到了解决. 主要包含4部分:JobTracker,TaskTracker,Task,Client ...
- p1694猴子 并查集
有n只猴子,第一只尾巴挂在树上,剩下的n-1只,要么被其他的猴子抓住,要么抓住了其他的猴子,要么两者均有. 当然一只猴子最多抓两只另外的猴子,因为只有两只猴爪子嘛.现在给出这n只猴子抓与被抓的信息,并 ...