hdu1829 A Bug's Life 基础种类并查集
题目的大意可以理解为:A爱B,B爱C ……给出一系列爱恋的关系,推断有没有同性恋。
思路是把相同性别的归为一个集合(等价类),异性的异性为同性。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
int f[N], r[N], flag;
void init()
{
for(int i=;i<N;i++)
{
f[i]=i;
r[i]=;
}
}
int Find(int x)
{
if(x==f[x]) return x;
return f[x]=Find(f[x]);
}
void Link(int x,int y)
{
int a=Find(x), b=Find(y);
if(a==b) flag=;
else
{
if(r[x]) f[y]=Find(r[x]);
else r[x]=y;
if(r[y]) f[x]=Find(r[y]);
else r[y]=x;
}
}
int main()
{
//freopen("test.txt","r",stdin);
int x,y,n,m,cas,k;
scanf("%d",&cas);
for(k=;k<=cas;k++)
{
scanf("%d%d",&n,&m);
init();
flag=;
while(m--)
{
scanf("%d%d",&x,&y);
if(!flag) continue;
Link(x,y);
}
printf("Scenario #%d:\n",k);
if(flag) printf("No suspicious bugs found!\n");
else printf("Suspicious bugs found!\n");
printf("\n");
}
return ;
}
hdu1829 A Bug's Life 基础种类并查集的更多相关文章
- hdu1829&&poj2492 A Bug's Life 基础种类并查集
把性别相同的虫子放在同一个集合,然后每读入一对虫子号,判断它们在不在同一集合,在则同性别,不在则继续 #include <cstdio> #include <cstring> ...
- 【进阶——种类并查集】hdu 1829 A Bug's Life (基础种类并查集)TUD Programming Contest 2005, Darmstadt, Germany
先说说种类并查集吧. 种类并查集是并查集的一种.但是,种类并查集中的数据是分若干类的.具体属于哪一类,有多少类,都要视具体情况而定.当然属于哪一类,要再开一个数组来储存.所以,种类并查集一般有两个数组 ...
- 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 ...
- POJ2492:A Bug's Life(种类并查集)
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 45757 Accepted: 14757 题 ...
- 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 ...
- hdu3038 How Many Answers Are Wrong【基础种类并查集】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4298091.html ---by 墨染之樱花 题目链接:http://acm.hdu.ed ...
- POJ-2492 A Bug's Life(种类并查集)
http://poj.org/problem?id=2492 题意: 给出一个T代表几组数据,给出一个n一个m,代表人的编号由1~n,m条命令,每条命令由两个数值组成,代表这两个人性别不同,问所有命令 ...
随机推荐
- 数据库连接池c3p0
XML配置文件: <?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <!- ...
- python PIL图像处理-图片上添加文字
首先需要安装库pillow cmd安装命令:pip install pillow 安装完后,编写脚本如下: from PIL import Image, ImageDraw, ImageFont de ...
- Hbuider sass配置 webstorm scss配置
--no-cache %FileName% ../css/%FileBaseName%.css sass编译后保存到css目录下 webstorm scss配置 C:\Ruby22-x64\bin ...
- 绝对好用的浏览器json解析网址
你们是否经常在浏览器输入请求地址解析遇到中文乱码的情况,今天我找到了一个好用的浏览器解析json网址,绝对好用. 1.直接输入网址 http://pro.jsonlint.com/ 2.输入要解析的j ...
- javascript实现:在N个字符串中找出最长的公子串
'use strict' module.exports = function 找出最长公子串 (...strings) { let setsOfSubstrings = [] strings.redu ...
- Oracle关联查询-数据类型不一致问题 ORA-01722: 无效数字
一.存在表A和表B,都包含字段user_no,但数据类型不一致,如下: create table A ( user_id varchar2(20), user_no number(12,0), xxx ...
- UEditor使用总结(与SpringMVC整合)
最近再弄富文本框,选择了UEditor,原因是:界面漂亮,百度开源的然而, 开启整合之路(怎么做) 1.下载插件 下载只有将插件放在Webapp下,如图 2.修改 导入之后我们就需要修改一些参数已满足 ...
- redis--周边知识点
一般Redis服务器内存超过128G内存的机器是非常少的 很少在redis中缓存视频,除非是快播,一般都是缓存文本字段 redis可视化的工具和SQL的管理工具是不一样的,最好是使用REDIS的she ...
- 获取Class对象方式
在java中,每个class都有一个相应的Class对象,当编写好一个类,编译完成后,在生成的.class文件中,就产生一个Class对象,用来表示这个类的类型信息.获得Class实例的三种方式: 1 ...
- mysql备份恢复中的常见错误
从A主机备份到B主机 mysqldump -uroot -p vw>vw.sql 现备份数据库文件,需要恢复到目标机B,B的数据库版本为5.5.23,A机器的mysql版本为5.0.2 ...