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

Problem Description
Background
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.

 
Input
The
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
The
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.
 
Sample Input
2
3 3
1 2
2 3
1 3
4 2
1 2
3 4
 
Sample Output
Scenario #1:
Suspicious bugs found!
Scenario #2:
No suspicious bugs found!
 
 
题意:
有若干bugs,一对一对的给出,判断其中是否有同性恋,如果1与2,2与3,1又与3,就说明1和3是gay.
代码:
 //将相同性别的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 并查集的更多相关文章

  1. poj1182、hdu1829(并查集)

    题目链接:http://poj.org/problem?id=1182 食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: ...

  2. A Bug's Life(hdu1829种类并查集)

    题意:有一群虫子,现在给你一些关系,判断这些关心有没有错 思路:向量种类并查集,下面讲一下向量的种类并查集 本题的各个集合的关心有两种0同性,1异性,怎么判断有错, 1.先判断他们是否在一个集合,即父 ...

  3. HDU1829(种类并查集)

    ps:本来是想找个二分图判断的题来写,结果百度到这鬼题 Problem Description Background Professor Hopper is researching the sexua ...

  4. HDU1829【种类并查集】

    题意: 检验给出条件是否有同性恋. 思路: 条件并查集. 还是一个类似的前缀和,sum[x]是x到根这段路径上的和,根一定是坐标越小的, 那么如果说对于同类(同一个集合)的判断就sum[a]是否等于s ...

  5. HDU-1829 A Bug's Life。并查集构造,与POJ1709异曲同工!

    A Bug's Life                                                     Find them, Catch them 都是并查集构造的题,不久前 ...

  6. hdu1829 A Bug&#39;s Life(并查集)

    开两个并查集.然后合并的时候要合并两次.这样在合并之前推断是否冲突,假设不冲突就进行合并,否则不须要继续合并. #include<cstdio> #include<cstdlib&g ...

  7. hdu 3038 How Many Answers Are Wrong(种类并查集)2009 Multi-University Training Contest 13

    了解了种类并查集,同时还知道了一个小技巧,这道题就比较容易了. 其实这是我碰到的第一道种类并查集,实在不会,只好看着别人的代码写.最后半懂不懂的写完了.然后又和别人的代码进行比较,还是不懂,但还是交了 ...

  8. POJ 2492 A Bug's Life【并查集高级应用+类似食物链】

    Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...

  9. BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]

    4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...

随机推荐

  1. java基础知识(三)java关键字

    关键字是电脑语言事先定义的,是特别意义的标识符,又叫保留字.用来表示一种数据类型或程序的结构等,关键字不能用作变量名.类名.方法名或参数.java目前共有50个关键字,其中"const&qu ...

  2. 转载:Android调用相册、拍照实现缩放、切割图片

    好几天没有写博客了,感觉都有点懈怠了.笔者参加了大学生第二届软件设计大赛,这几天 一直在弄大赛的事情,没有花些时间来整理博客.好在经过一些时日比赛的东西也弄得差不多了, 接下来就是将这段时间学习里面有 ...

  3. extractor

    package scrollable.excel.reader; import java.io.IOException; import java.io.InputStream; import java ...

  4. word20161215

    name / 名称 name mapping / 名称映射 name resolution / 名称解析 name server (NS) resource record / 名称服务器资源记录 na ...

  5. python FileError

    >>> ls1=["nihia"] >>> ls1 ['nihia'] >>> ls1.pop() 'nihia' >& ...

  6. ASP.NET MVC中的两个Action之间值的传递--TempData

    一. ASP.NET MVC中的TempData 在ASP.NET MVC框架的ControllerBase中存在一个叫做TempData的Property,它的类型为TempDataDictiona ...

  7. nginx笔记资料

    通配 hash 表 ngx_hash_init 实现注释:http://blog.csdn.net/gsnumen/article/details/7817396 ngx_hash_init之后的结构 ...

  8. 曲线救国:IIS7集成模式下如何获取网站的URL

    如果我们在Global中的Application_Start事件中访问HttpContext.Current.Request对象,如: protected void Application_Start ...

  9. Revit 自定义RibbonPanel到Revit中

    项目下找到TheApplication.cs,更改内容: class TheApplication : IExternalApplication    {        public Result O ...

  10. 【转】Caffe初试(五)视觉层及参数

    本文只讲解视觉层(Vision Layers)的参数,视觉层包括Convolution, Pooling, Local Response Normalization (LRN), im2col等层. ...