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. 1、Visual Studio Code安装及Hello Word

    一.环境初始化 1.下载 Visual Studio Code对应版本安装 2.下载.NET Core 2.0 SDK安装 3.安装Mono Debug   完成后界面如下:       二.创建控制 ...

  2. Android TV 选中高亮显示

    1.开发Android TV APP, 使用遥控器选中按钮或者选着其它菜单 如果没有高亮显示,就看不出选中哪个按钮或者菜单 2.在drawable 添加 border_red.xml 设置选中高亮 & ...

  3. Hbase 简单记录

    进入hbase 客户端命令行: hbase shell 根据rowkey获取单条数据: get 'HXXT_ns:app_test_hbase', 'rowkey值'   范围查询,并指定数据量: s ...

  4. Table is specified twice, both as a target for 'UPDATE' and as a separate source

    UPDATE Bins b SET b.ShopSn =’111201611111168706’ WHERE b.Id IN (SELECT b.Id FROM Bins b JOIN BinInve ...

  5. redis 主从复制 redis 探索

    http://blog.csdn.net/column/details/12804.html

  6. Linux while和for循环简单分析

    一.循环重定向 最近遇到了一种新的循环重定向写法,由于没看懂,说以网上搜索了一下,然后再此分享一下: while read line do ...... done  < file 刚开始看,不明 ...

  7. THREE.js代码备份——webgl - custom attributes [lines](自定义字体显示、控制字图的各个属性)

    <!DOCTYPE html> <html lang="en"> <head> <title>three.js webgl - cu ...

  8. eas之dep的前置脚本和后置脚本

    dep的前置脚本和后置脚本,什么时候写,是这样解释的:    前置脚本是在方法前执行,后置脚本是在方法后执行    1.比如保存扩展,如果你要在保存前校验某个字段的值,你要在前置脚本中写,如果要保存后 ...

  9. Java实验环境搭建

    1.JDK的下载一.JDK的下载及安装 (1).网站网址搜索http://www.oracle.com/technetwork/java,进入浏览页(2)找到Trials and Download 点 ...

  10. C#第十一节课

    类 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Thr ...