*HDU1829 并查集

时间:2022-09-22 18:17:17

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&&num;39&semi;s Life&lpar;hdu1829种类并查集&rpar;

    题意:有一群虫子,现在给你一些关系,判断这些关心有没有错 思路:向量种类并查集,下面讲一下向量的种类并查集 本题的各个集合的关心有两种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&amp&semi;&num;39&semi;s Life&lpar;并查集&rpar;

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

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

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

  8. POJ 2492 A Bug&&num;39&semi;s Life【并查集高级应用&plus;类似食物链】

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

  9. BZOJ 4199&colon; &lbrack;Noi2015&rsqb;品酒大会 &lbrack;后缀数组 带权并查集&rsqb;

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

随机推荐

  1. 详解DB2 sqlstate 57016 原因码 &quot&semi;7&quot&semi;错误

  2. android&colon;id&equals;&quot&semi;&commat;&plus;id&sol;button1&quot&semi; 与 android&colon;id&equals;&quot&semi;&commat;id&sol;button1&quot&semi; 区别 &commat;string

    一.android:id="@+id/button1" 与 android:id="@id/button1" 区别 android:id="@+id/ ...

  3. Android&lowbar;gridView&lowbar;LIstener&lowbar;examle

    layout.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" x ...

  4. Dubbo在Spring和Spring Boot中的使用

    一.在Spring中使用Dubbo 1.Maven依赖 <dependency> <groupId>com.alibaba</groupId> <artifa ...

  5. GC的前世与今生

    GC的前世与今生 虽然本文是以.net作为目标来讲述GC,但是GC的概念并非才诞生不久.早在1958年,由鼎鼎大名的图林奖得主John McCarthy所实现的Lisp语言就已经提供了GC的功能,这是 ...

  6. RF&plus;Selenium2Library&plus;Sikuli集成环境搭建

    Sikuli是通过截图来编写代码的脚本语言,他是对于Selenium不好处理的一些模态窗口.flash等的利器.废话少说,直接开始安装吧.安装RF+Selenium2Library的环境这里就不说了, ...

  7. linux中编译安装Apache、PHP、MySQL(上)

    1.简介 在阿里云买了个云服务器,一直没时间折腾.过了近十天了吧,才有时间好好玩玩这个云服务器.自己是做Web开发的,所以我需要的开发环境是LAMP.之前打算是采用yum安装,不过yum安装apach ...

  8. templet模式

    package template;import java.sql.Connection;import java.sql.ResultSet;/** * Created by marcopan on 1 ...

  9. SQL根据细粒度为天的查询

    当我们集成了一些前端框架,在某些展示页面上往往具有某些查询条件.而这其中日期查询的处理又较为麻烦,此处,我罗列了一种当前台上传了一种默认的date格式的日期查询数据至后台未经Controller或Se ...

  10. java中一些对象&lpar;po&comma;vo&comma;dao&comma;pojo&rpar;等的解释

     一.PO:persistant object 持久对象,可以看成是与数据库中的表相映射的java对象.最简单的PO就是对应数据库中某个表中的一条记录,多个记录可以用PO的集合.PO中应该不包含任何对 ...