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,和父节点同性别 并查集一 ...
随机推荐
- HDU 1423 Greatest Common Increasing Subsequence(LICS入门,只要求出最长数)
Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- mysql有哪几种索引
从数据结构角度 1. B+树索引(O(log(n))) 2. hash索引 3. FULLTEXT索引 4. R-Tree索引 从物理存储角度 1. 聚集索引 2. 非聚集索引 从逻辑角度 ...
- Android图表AChartEngine
很多时候项目中我们需要对一些统计数据进行绘制表格,更多直观查看报表分析结果.基本有以下几种方法: 1:可以进行android api进行draw这样的话,效率比较低 2:使用开源绘表引擎,这样效率比较 ...
- PyOpenGL下GlutBitmapCharacter的替代
虽然pyinstaller支持pyopengl,但是调用GLUT之后,在其它电脑上面运行就会出现错误,索性按照之前C#上面的办法,把字体数据和函数用python重写 fontData.py #! /u ...
- java8--面向对象 下(疯狂java讲义3) 复习笔记
1.如果一个包装类和一个基本类型比较大小,或者是两个基本类型进行比较大小,直接用==就好: 如果是两个包装类进行比较大小,那么使用equals(),返回值是true,false,或者使用Xxx.com ...
- mysql优化---in型子查询,exists子查询,from 型子查询
in型子查询引出的陷阱:(扫更少的行,不要临时表,不要文件排序就快) 题: 在ecshop商城表中,查询6号栏目的商品, (注,6号是一个大栏目) 最直观的: mysql); 误区: 给我们的感觉是, ...
- centos 网络连接查看
安装iftop. 这个得先安装epel #yum install epel-replease -y #yum install iftop -y #iftop
- 解决ubuntu10.04不能上网
1:命令行输入:lspci查看驱动,最后几行如果有ethernet controller:atheros communications ar8151 v1.0*的话,就说明驱动没有安装好, 2:下载地 ...
- I2S总线协议理解
I2S总线 Inter IC Sound总线又称集成电路内置音频总线. I2S对数字音频设备之间的音频数据传输而制定的一种总线标准. 采用了沿独立的导线传输时钟与数据信号的设计,通过将数据和时钟信号分 ...
- POJ1860 Currency Exchange —— spfa求正环
题目链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Tota ...