A Bug's Life

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

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!

题目大意:给你若干个bug,保证他们是异性,看最后有没有同性恋‘
思路分析:基础并查集,维护与根节点的关系即可
poj1703 同类型
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=+;
int fa[maxn];
int relate[maxn];//记录与父节点的性别关系,1代表异性,0代表同性
int n,m;
int kase=;
int root(int x)
{
if(x==fa[x]) return x;
int t=root(fa[x]);
relate[x]=(relate[x]+relate[fa[x]])%;
fa[x]=t;
return fa[x];
}
void merge(int x,int y)
{
int fx=root(x);
int fy=root(y);
if(fx==fy) return;
fa[fx]=fy;
relate[fx]=(relate[y]+relate[x]+)%;
return;
}
int main()
{
int T;
int a,b;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
fa[i]=i,relate[i]=;
bool flag=false;
for(int j=;j<=m;j++)
{
scanf("%d%d",&a,&b);
if(root(a)==root(b)&&relate[a]==relate[b])
{
flag=true;
}
else merge(a,b);
//printf("yes\n");
}
//printf("dsasd\n");
printf("Scenario #%d:\n",++kase);
if(flag) printf("Suspicious bugs found!\n\n");
else printf("No suspicious bugs found!\n\n"); }
return ;
}
 

hdu 1829 基础并查集,查同性恋的更多相关文章

  1. HDU(3560)成环,并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3560 并查集查有几个块,修改了之前我的一个方法(用什么map),直接判断根节点的id是i的个数. 然后 ...

  2. BZOJ1202 [HNOI2005]狡猾的商人 【并查集】

    1202: [HNOI2005]狡猾的商人 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 4180  Solved: 2015 [Submit][S ...

  3. BZOJ 3319: 黑白树 并查集 + 离线 + 思维

    Description 给定一棵树,边的颜色为黑或白,初始时全部为白色.维护两个操作: 1.查询u到根路径上的第一条黑色边的标号. 2.将u到v    路径上的所有边的颜色设为黑色. Notice:这 ...

  4. PAT Advanced A1021 Deepest Root (25) [图的遍历,DFS,计算连通分量的个数,BFS,并查集]

    题目 A graph which is connected and acyclic can be considered a tree. The height of the tree depends o ...

  5. PAT Advanced 1013 Battle Over Cities (25) [图的遍历,统计连通分量的个数,DFS,BFS,并查集]

    题目 It is vitally important to have all the cities connected by highways in a war. If a city is occup ...

  6. PAT Advanced 1034 Head of a Gang (30) [图的遍历,BFS,DFS,并查集]

    题目 One way that the police finds the head of a gang is to check people's phone calls. If there is a ...

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

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

  8. 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条臭 ...

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

随机推荐

  1. 转:使用XHProf优化PHP程序

    原文来自于:http://blog.sina.com.cn/s/blog_665fc8980100l8dq.html XHProf 是 FaceBook 开发的一个函数级别的 PHP 分层分析器. 数 ...

  2. Java中匿名类的两种实现方式(转)

    使用匿名内部类课使代码更加简洁.紧凑,模块化程度更高.内部类能够访问外部内的一切成员变量和方法,包括私有的,而实现接口或继承类做不到.然而这个不是我说的重点,我说的很简单,就是匿名内部类的两种实现方式 ...

  3. COJN 0583 800602分苹果

    800602分苹果 难度级别:B: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放, ...

  4. -_-#【Canvas】绘制文本

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  5. awk工具

    简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再 ...

  6. 【转】Android的权限permission

    原文网址:http://blog.csdn.net/nei504293736/article/details/6974599 <uses-permission android:name=&quo ...

  7. Hyper-V连接虚拟机异常,“无法进行连接,因为可能无法将凭据发送到远程计算机”

    前两天把公司的TFS从2010升级到TFS2012(昨天又升级到TFS2013).今天使用服务器Hyper-V连接虚拟机时居然报错了. 翻看各种日志,虽然错误大一对一对地,但找不到任何有用的信息.无奈 ...

  8. gcc编译命令

    g++ demo.cpp -o demo.exe

  9. UVA11995【I can guess the data structrue!!】【水】+UVA11991【map用法】

    先看UVA11995 两份代码一份直接用C写的,一份用STL写的 #include <iostream> #include <stdio.h> #include <str ...

  10. SOAP详解

    1. SOAP简介 1.1应用背景 对于应用程序开发来说,使程序之间进行因特网通信是很重要的.目前的应用程序通过使用远程过程调用(RPC)在诸如 DCOM 与 CORBA 等对象之间进行通信,但是 H ...