A Bug's Life

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9204    Accepted Submission(s): 2961

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!

Hint

Huge input,scanf is recommended.

 
Source

题意:

1喜欢2,2喜欢3,而1又喜欢3,则矛盾。 (排出同性恋)

基础并查集的新应用:

分组并查集(偏移量):开一个并查集,但是要加个偏移向量数组,来记录每个节点距离根节点的距离

代码:

 /*带权值的并查集*/
#include<cstdio>
#define maxn 2005
int father[maxn],rank[maxn];
int tt,nn,mm;
void init()
{
for(int i=;i<nn;i++){
father[i]=i;
rank[i]=;
}
} int fin(int x)
{
if(x==father[x]) return x;
int temp=fin(father[x]);
rank[x]=(rank[x]+rank[father[x]])%;
father[x]=temp;
return father[x];
}
int unin(int a,int b)
{
int x=fin(a);
int y=fin(b);
if(x==y)
{
if(rank[a]==rank[b])return ; //说明矛盾
return ;
}
father[x]=y;
rank[x]=(rank[a]+rank[b]+)%;
return ;
}
int main()
{
int a,b;
int tag;
scanf("%d",&tt);
for(int j=;j<=tt;j++)
{
tag=;
scanf("%d%d",&nn,&mm);
init();
for(int i=;i<mm;i++)
{
scanf("%d%d",&a,&b);
tag+=unin(a,b);
}
printf("Scenario #%d:\n",j); if(tag)puts("Suspicious bugs found!");
else puts("No suspicious bugs found!");
puts("");
}
return ;
}

hdu 1829 A Bug's Life(分组并查集(偏移量))的更多相关文章

  1. HDU 1829 A Bug's Life(种类并查集)

    思路:见代码吧. #include <stdio.h> #include <string.h> #include <set> #include <vector ...

  2. hdoj 1829 A bug's life 种类并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 并查集的一个应用,就是检测是否存在矛盾,就是两个不该相交的集合有了交集.本题就是这样,一种虫子有 ...

  3. HDU 1829 分组并查集

    题意:有两种性别,每组数据表示是男女朋友,判断输入的几组数据是否有同性恋 思路:http://blog.csdn.net/iaccepted/article/details/24304087 分组并查 ...

  4. 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 ...

  5. hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them

    http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...

  6. 【进阶——种类并查集】hdu 1829 A Bug's Life (基础种类并查集)TUD Programming Contest 2005, Darmstadt, Germany

    先说说种类并查集吧. 种类并查集是并查集的一种.但是,种类并查集中的数据是分若干类的.具体属于哪一类,有多少类,都要视具体情况而定.当然属于哪一类,要再开一个数组来储存.所以,种类并查集一般有两个数组 ...

  7. hdu 1829 A Bug's Life(并查集)

                                                                                                    A Bu ...

  8. HDU 1829 - A Bug's Life

    Problem Description Background Professor Hopper is researching the sexual behavior of a rare species ...

  9. HDU 1829 A Bug's Life 【带权并查集/补集法/向量法】

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

随机推荐

  1. Cheatsheet: 2013 08.20 ~ 08.31

    .NET Protobuf-net: the unofficial manual 5 Common C# Misconceptions What is new in the Mono project ...

  2. 关于float的感悟

    给元素设置了float样式后,最终的结果是: 1:这个元素漂浮起来, 2:其他的元素位置可以视为 这个元素不存在 的时候的位置:但是float样式还是对整个页面有所影响 3:float的影响就是他附近 ...

  3. Bson

    https://en.wikipedia.org/wiki/BSON BSON /ˈbiːsɒn/ is a computer data interchange format used mainly ...

  4. QT 加载c语言编译的动态库

    QLibrary lib("./libprint.so");//库的路径if(lib.load()){    typedef void(*AddFunction)(char *st ...

  5. kvc kvo(摘录)

    概述 由于ObjC主要基于Smalltalk进行设计,因此它有很多类似于Ruby.Python的动态特性,例如动态类型.动态加载.动态绑定等.今天我们着重介绍ObjC中的键值编码(KVC).键值监听( ...

  6. kafka单节点部署无法访问问题解决

    场景:在笔记本安装了一台虚拟机, 在本地的虚拟机上部署了一个kafka服务: 写了一个测试程序,在笔记本上运行测试程序,访问虚拟机上的kafka,报如下异常: 2015-01-15 09:33:26 ...

  7. web 模板 类似京东左侧的导航栏

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. openstack 排错

    1.查看日志 grep ERROR /var/log/keystone/keystone.log 2. # nova list ERROR:n/a (http 404) 检查环境变量是否正确.

  9. iOS - Plist 数据解析

    前言 NS_AVAILABLE(10_6, 4_0) @interface NSPropertyListSerialization : NSObject 如果对象是 NSArray 或 NSDicti ...

  10. c point

    a[i] 与 *(a+i) 是等价的. 事实上在计算a[i]的值时,c语言首先将前者转换为后者形式, 而且,通常而言,用指针编写的程序要比用数组下标编写的程序执行速度快,(为什么?) 因此,应该尽量用 ...