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. Hibernate的查询语言之HQL(二)——Hibernate查询的from字句

    from 是最简单的HQL语句,也是最基本的HQL语句.from 关键字后紧跟持久化类的类名.例如: from Person 表明从Person持久化类中取出全部的实例. 大部分时候,推荐位该Pers ...

  2. 自然语言处理(5)之Levenshtein最小编辑距离算法

    自然语言处理(5)之Levenshtein最小编辑距离算法 题记:之前在公司使用Levenshtein最小编辑距离算法来实现相似车牌的计算的特性开发,正好本节来总结下Levenshtein最小编辑距离 ...

  3. poj Candies

    http://poj.org/problem?id=3159 #include<cstdio> #include<queue> #include<cstring> ...

  4. 疯狂java实战演义 弹球游戏代码

    package org.crazyit.ball; import java.awt.Image; import java.io.File; import javax.imageio.ImageIO; ...

  5. Qt 中如何捕获窗口停用和激活的消息

    最近一直在用Qt做一个简单的俄罗斯方块的游戏,由于要实现一个暂停游戏的功能,就是当鼠标移出正在运行的游戏,点击电脑桌面上的其他位置时,这个时候游戏暂停.在这里把实现过程简单的记录一下,作为一个学习笔记 ...

  6. linux shell 终端中文乱码(转)

    方法一:修改/etc/sysconfig/i18n 文件把里面的LANG="en_US"改成 GB2312就可以了要重启一下机器不用重启的方法,直接# LANG="GB2 ...

  7. bzoj3028食物

    http://www.lydsy.com/JudgeOnline/problem.php?id=3028 好吧,这是我第一道生成函数的题目. 先搞出各种食物的生成函数: 汉堡:$1+x^2+x^4+. ...

  8. freemarker对数字的处理

    freemark会默认对数字进行格式化处理,例如price = 12000,  通过${price}显示为12,000,  但其实有些场景会有问题: 比如编辑一条记录, 再保存,容易将12,000传到 ...

  9. redis在Java web项目的简单应用(转载)

    看到一篇关于redis和spring集成的文章,实际测试后,可以.转载以备用.谢谢 亲昵YY! html,body { font-size: 15px } body { font-family: He ...

  10. RESTFul中的那些事(1)---在RESTFul中,HTTP Put和Patch操作的差别?

    笔者在用调用Google Calendar和Google Tasks的RESTFul API的时候.遇到了一个特殊的操作,PATCH. 那么PATCH操作和PUT操作的差别是什么呢? 依据PATCH ...