*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 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
随机推荐
- hadoop集群安装_实战
spark1.6.2+ hadoop2.6.2 词频统计完整案例:http://blog.csdn.net/zythy/article/details/17852579 hadoop学习:http:/ ...
- Java开发规范摘录
对于规范的 JAVA 派生类,尽量用 eclipse工具来生成文件格式,避免用手写的头文件/实现文件. 尽量避免一行的长度超过 200 个字符,因为很多终端和工具不能很好处理之.缩进8格 ,impor ...
- 记 Mac Pro 系统升级后,编译安装 PHP-5.6.28 / PHP-7.0 报错修复过程
买 Mac Pro 的时候,系统为 OS X 10.11.5,编译 PHP-5.6.21 的时候,也遇到一些坑,安装过程记录如下: Mac Pro 编译安装 PHP 5.6.21 及 问题汇总 后来, ...
- Keepalived日志
默认日志存放在系统日志:/var/log/messages下 [root@lb01 /]# tail -f /var/log/messages Oct :: lb01 Keepalived_vrrp[ ...
- 【转】MVC中处理Json和JS中处理Json对象
事实上,MVC中已经很好的封装了Json,让我们很方便的进行操作,而不像JS中那么复杂了. MVC中: public JsonResult Test() { JsonResult json = new ...
- 学习JVM GarbageCollection
前言 Java和C++之间显著的一个区别就是对内存的管理.Java和C++把内存管理的权利赋予给开发人员的方式不同,Java拥有一套自动的内存回收系统(Garbage Collection,GC)简称 ...
- 【安装Nodejs】CentOS7 下安装NodeJs+Express+MongoDB+Redis
最近想拿NodeJS做个文档管理系统玩玩,看看mongdb的gridfs效率咋样,谁晓得因为一个Yeoman的脚手架,整来整去的把文件权限全部搞乱,一想算了,还是重来搞一套吧! 1.安装 yum i ...
- tp5 中 model 的新增方法
//默认主键为自动识别,如果需要指定,可以设置属性: namespace app\index\model; use think\Model; class User extends Model { pr ...
- .NET LINQ 联接运算
联接运算 将两个数据源“联接”就是将一个数据源中的对象与另一个数据源中共享某个通用特性的对象关联起来. 当查询所面向的数据源相互之间具有无法直接领会的关系时,联接就成为一项重要的运 ...
- appCan uexLocation 定位功能
js的引用: <script src="../js/zy_control.js"></script> <script src="../js/ ...