A Bug's Life

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 12677    Accepted Submission(s): 4142
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
 
Recommend
linle   |   We have carefully selected several similar problems for you:  1558 1811 1198 1856 1325 

//题意:给出了1--n的m种关系,x--y中x跟y的性别未知,问有没有可能出现同性恋
//一道带权并查集问题, 不要考虑x,y的性别是男是女,只需要考虑他们的性别
//是否一致, vis数组存放爱人的性别,
//举例子:x--y说明x和y有关系
//如果说vis[x]不为零说明x有了爱人,并且vis[x]存放的就是他爱人的性别,
//那么 那么y一定跟vis[x]是一个集合中的,不需要管vis存放的都是什么数字
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define MAXN 10000010
int n,m,flag;
int vis[MAXN],pre[MAXN];
void init()
{
for(int i=0;i<=n;i++)
{
vis[i]=0;
pre[i]=i;
}
}
int find(int x)
{
if(pre[x]!=x)
pre[x]=find(pre[x]);
return pre[x];
}
void join(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx==fy)
flag=1;
else
{
if(vis[fx])//如果说fx已经有了爱人,那么fy的性别一定要和fx性别一样
pre[vis[fx]]=fy;
if(vis[fy])
pre[vis[fy]]=fx;
vis[fx]=fy;vis[fy]=fx;//vis存放爱人的性别,fx爱人的性别是fy
//如果说两人性别不属于同一结合,两人可以相恋
}
}
int main()
{
int t,k=1;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();
flag=0;
int x,y;
for(int i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
join(x,y);
}
printf("Scenario #%d:\n",k++);
if(flag)
printf("Suspicious bugs found!\n\n");
else
printf("No suspicious bugs found!\n\n");
}
return 0;
}

hdoj--1829--A Bug's Life(带权并查集)的更多相关文章

  1. POJ2492 A Bug's Life 带权并查集

    分析:所谓带权并查集,就是比朴素的并查集多了一个数组,记录一些东西,例如到根的距离,或者和根的关系等 这个题,权数组为relation 代表的关系  1 和父节点不同性别,0,和父节点同性别 并查集一 ...

  2. POJ 2492 A Bug's Life 带权并查集

    题意: 思路: mod2 意义下的带权并查集 如果两只虫子是异性恋,它们的距离应该是1. 如果两只虫子相恋且距离为零,则它们是同性恋. (出题人好猥琐啊) 注意: 不能输入一半就break出来.... ...

  3. poj2492A Bug's Life——带权并查集

    题目:http://poj.org/problem?id=2492 所有元素加入同一个并查集中,通过其偏移量%2将其分类为同性与异性,据此判断事件. 代码如下: #include<iostrea ...

  4. POJ 2492 A Bug's Life (带权并查集 && 向量偏移)

    题意 : 给你 n 只虫且性别只有公母, 接下来给出 m 个关系, 这 m 个关系中都是代表这两只虫能够交配, 就是默认异性, 问你在给出的关系中有没有与异性交配这一事实相反的, 即同性之间给出了交配 ...

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

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

  6. hdu 1829 &amp;poj 2492 A Bug&#39;s Life(推断二分图、带权并查集)

    A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  7. hdu 1829-A Bug's LIfe(简单带权并查集)

    题意:Bug有两种性别,异性之间才交往, 让你根据数据判断是否存在同性恋,输入有 t 组数据,每组数据给出bug数量n, 和关系数m, 以下m行给出相交往的一对Bug编号 a, b.只需要判断有没有, ...

  8. A Bug's Life POJ - 2492 (带权并查集)

    A Bug's Life POJ - 2492 Background Professor Hopper is researching the sexual behavior of a rare spe ...

  9. poj2492 A Bug's Life(带权并查集)

    题目链接 http://poj.org/problem?id=2492 题意 虫子有两种性别,有n只虫子,编号1~n,输入m组数据,每组数据包含a.b两只虫子,表示a.b为不同性别的虫子,根据输入的m ...

随机推荐

  1. Linux的那点事

    1.重启nginx服务器 注意,修改了nginx配置文件后最好先检查一下修改过的配置文件是否正确,以免重启后Nginx出现错误影响服务器稳定运行. 判断Nginx配置是否正确命令如下: nginx - ...

  2. CSS的常用属性(一)

    文本属性 font-size: 16px 文字大小 font-weight: 700 文字粗细 值从100-900 (值为700看上去加粗了) 不推荐使用font-weight: bold font- ...

  3. js---html元素操作

    思路:创给节点添加一个元素:步骤: 1:创建元素节点 2:创建文本节点 3:将该文本添加到元素上 4:将该元素追加到其他元素上 appendChild() 方法,将新元素作为父元素的最后一个子元素进行 ...

  4. Qt无法用UTF-8编辑问题

    原因: Windows默认编码格式是GBK. 而QT-各默认版本的编码格式是UTF-8. 解决方法": Windows环境下,Qt Creator,菜单->工具->选项-> ...

  5. Caffe2:ubuntuKylin17.04使用Caffe2.LSTM

    一早发现caffe2的较成熟的release版发布了(the first production-ready release),那么深度学习平台在之后一段时间也是会出现其与tensorflow相互竞争的 ...

  6. [Intermediate Algorithm] - Everything Be True

    题目 所有的东西都是真的! 完善编辑器中的every函数,如果集合(collection)中的所有对象都存在对应的属性(pre),并且属性(pre)对应的值为真.函数返回ture.反之,返回false ...

  7. Windows Live Writer 历史Blog修改的功能

    其实 WLW 有历史Blog修改的功能,我只是一直没有找到,就在打开“最近发布的日志”里面, 位于屏幕的右侧“打开”列表下. 最近发现记忆力越来越差了,BLOG看来是必须的了.

  8. Cache-Control 机制是为浏览器定制的?

    Cache-Control 机制是为浏览器定制的?

  9. Steal 偷天换日 题解(From luoguBlog)

    树形+背包 奇奇怪怪的dp. 考试的时候费了半天劲把题读完后思路基本正解, 然而也不知道为什么脑子鬼畜了一下打了个非递归建树? 而且链式前向星建边? 岔路口和藏品都搞成节点? 自己给自己找麻烦Orz. ...

  10. eas之去掉关闭eas页面时校验是否修改的提示

    EditUI-------> public boolean checkBeforeWindowClosing() {            boolean b = super.checkBefo ...