*HDU1829 并查集
A Bug's Life
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14121 Accepted Submission(s): 4603
Professor
Hopper is researching the sexual behavior of a rare species of bugs. He
assumes that they feature two different genders and that they only
interact with bugs of the opposite gender. In his experiment, individual
bugs and their interactions were easy to identify, because numbers were
printed on their backs.
Problem
Given a list of bug
interactions, decide whether the experiment supports his assumption of
two genders with no homosexual bugs or if it contains some bug
interactions that falsify it.
first line of the input contains the number of scenarios. Each scenario
starts with one line giving the number of bugs (at least one, and up to
2000) and the number of interactions (up to 1000000) separated by a
single space. In the following lines, each interaction is given in the
form of two distinct bug numbers separated by a single space. Bugs are
numbered consecutively starting from one.
output for every scenario is a line containing "Scenario #i:", where i
is the number of the scenario starting at 1, followed by one line saying
either "No suspicious bugs found!" if the experiment is consistent with
his assumption about the bugs' sexual behavior, or "Suspicious bugs
found!" if Professor Hopper's assumption is definitely wrong.
//将相同性别的bugs放进同一并查集里,这样只要输入的两个bug有相同的根就是gay.
//路径压缩的while 的find比递归的find稍快一些。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int t,n,m;
int fat[],sex[];
int find(int x)
{
int rt=x;
while(fat[rt]!=rt)
rt=fat[rt];
int i=x,j;
while(i!=rt)
{
j=fat[i];
fat[i]=rt;
i=j;
}
return rt;
}
/*int find(int x)
{
if(fat[x]!=x)
fat[x]=find(fat[x]);
return fat[x];
}*/
void connect(int x,int y)
{
int xx=find(x),yy=find(y);
if(xx!=yy)
fat[xx]=yy;
}
int main()
{
int a,b;
scanf("%d",&t);
for(int k=;k<=t;k++)
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
fat[i]=i;
sex[i]=;
}
bool flag=;
for(int i=;i<=m;i++)
{
scanf("%d%d",&a,&b);
if(flag) continue;
if(find(a)==find(b))
{
flag=;
continue;
}
if(sex[a]==) sex[a]=b;
else connect(sex[a],b);
if(sex[b]==) sex[b]=a;
else connect(sex[b],a);
}
printf("Scenario #%d:\n",k);
if(flag) printf("Suspicious bugs found!\n");
else printf("No suspicious bugs found!\n");
printf("\n");
}
return ;
}
*HDU1829 并查集的更多相关文章
- poj1182、hdu1829(并查集)
题目链接:http://poj.org/problem?id=1182 食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: ...
- A Bug's Life(hdu1829种类并查集)
题意:有一群虫子,现在给你一些关系,判断这些关心有没有错 思路:向量种类并查集,下面讲一下向量的种类并查集 本题的各个集合的关心有两种0同性,1异性,怎么判断有错, 1.先判断他们是否在一个集合,即父 ...
- HDU1829(种类并查集)
ps:本来是想找个二分图判断的题来写,结果百度到这鬼题 Problem Description Background Professor Hopper is researching the sexua ...
- HDU1829【种类并查集】
题意: 检验给出条件是否有同性恋. 思路: 条件并查集. 还是一个类似的前缀和,sum[x]是x到根这段路径上的和,根一定是坐标越小的, 那么如果说对于同类(同一个集合)的判断就sum[a]是否等于s ...
- HDU-1829 A Bug's Life。并查集构造,与POJ1709异曲同工!
A Bug's Life Find them, Catch them 都是并查集构造的题,不久前 ...
- hdu1829 A Bug's Life(并查集)
开两个并查集.然后合并的时候要合并两次.这样在合并之前推断是否冲突,假设不冲突就进行合并,否则不须要继续合并. #include<cstdio> #include<cstdlib&g ...
- hdu 3038 How Many Answers Are Wrong(种类并查集)2009 Multi-University Training Contest 13
了解了种类并查集,同时还知道了一个小技巧,这道题就比较容易了. 其实这是我碰到的第一道种类并查集,实在不会,只好看着别人的代码写.最后半懂不懂的写完了.然后又和别人的代码进行比较,还是不懂,但还是交了 ...
- POJ 2492 A Bug's Life【并查集高级应用+类似食物链】
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
随机推荐
- Reader与InputStream两个类中的read()的区别
InputStream类的read()方法是从流里面取出一个字节,他的函数原型是 int read(); ,Reader类的read()方法则是从流里面取出一个字符(一个char),他的函数原型也是 ...
- mrjob 使用 mongodb 作为数据源
When using a mongoDB collection as input, add the arguments -jobconf mongo.input.uri=<input mongo ...
- Android模拟位置信息
Android模拟位置程序,俗称GPS欺骗,只能修改采用GPS定位的软件. 手机定位方式目前有4种:基站定位,WIFI定位,GPS定位,AGPS定位 常见的修改手法: 1. 抓包欺骗法,抓包改包欺骗服 ...
- robotframework----模板的使用
模板只能放在测试用例里,可以测试大数据量,每一行模板的值,都做为用户关键字的输入参数,发下图: 删除特定证书2 的用户关键字如下:
- mac下wifi无法连接的问题
今天遇到了一个Wi-Fi打死连不上的问题,关闭重启电脑路由器都试了一下还是不行,最后把资源库/偏好设置/SystemConfiguration下的文件都删除,有一个是删不掉的,留着不影响,然后重启,O ...
- SQL Server 临时禁用和启用所有外键约束(高版本向低版本迁移数据)
--获得禁用所有外键约束的语句 select 'ALTER TABLE [' + b.name + '] NOCHECK CONSTRAINT ' + a.name +';' as 禁用约束 from ...
- 51nod1073(约瑟夫环)
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1073 题意: 中文题诶~ 思路: 直接模拟的话O(n*k)的 ...
- Android平台下OpenCV移植与使用---基于C/C++
在<Android Studio增加NDK代码编译支持--Mac环境>和<Mac平台下Opencv开发环境搭建>两篇文章中,介绍了如何使用NDK环境和Opencv环境搭建与测试 ...
- MSSQL 查询表空间
1. exec sp_spaceused '表名' (SQL统计数据,大量事务操作后可能不准) 2. exec sp_spaceused '表名', true (更新 ...
- ****CI框架源码阅读笔记7 配置管理组件 Config.php
http://blog.csdn.net/ohmygirl/article/details/41041597 一个灵活可控的应用程序中,必然会存在大量的可控参数(我们称为配置),例如在CI的主配置文件 ...