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

 #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. 诊断并解决 ORA-4030 错误 (Doc ID 1548826.1)

    适用于: Oracle Database - Enterprise Edition - 版本号 8.1.7.4 和更高版本号 本文档所含信息适用于全部平台 用途 怎样诊断 ORA-4030 错误 排错 ...

  2. 基于bootstrap_登陆页面

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. Codeforces Round #335 (Div. 2) 606B Testing Robots(模拟)

    B. Testing Robots time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  4. Vue 之 npm 及 安装的包

    1  npm相关 1.1 npm 是 基于Node.js 的,所以要先安装Node.js 在浏览器地址栏输入https://nodejs.org/en/, 进入Node.js官网后,点击下载左边的稳定 ...

  5. quilt

    1 什么是quilt quilt是一个patch管理工具,特别适合于对多个patch进行管理. quilt是基于gnu patch和diff的. 2 使用quilt创建一个patch 第一步,quil ...

  6. Record is locked by another user

    Oracle修改表中记录时出现record is locked by another user的问题 在操作表时没有commit,导致表被锁,只要执行下面两行语句,就可以了将行锁解锁了. Select ...

  7. 阿里云短信服务发送短信验证码(JAVA开发此功能)

    开发此功能需注册阿里云账号,并开通短信服务(免费开通) 充值后,不会影响业务的正常使用!(因为发送验证类短信:1-10万范围的短信是0.045元/条).开发测试使用,充2块钱测试足够了 可参考阿里云官 ...

  8. CentOS/Ubuntu安装GLIBCXX3.4.21

    经过测试“GLIBCXX3.4.21 not find”这篇博文解决了我的问题. 以下是安装步骤:   一.首先查看当前gcc版本 strings /usr/lib/x86_64_linux-gun/ ...

  9. 广大暑假训练1(poj 2488) A Knight's Journey 解题报告

    题目链接:http://vjudge.net/contest/view.action?cid=51369#problem/A   (A - Children of the Candy Corn) ht ...

  10. android.annotation.SuppressLint

    Lint是一个静态检查器,它围绕Android项目的正确性.安全性.性能.可用性以及可访问性进行分析.它检查的对象包括XML资源.位图.ProGuard配置文件.源文件甚至编译后的字节码. Lint包 ...