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. IOS 多个UIImageView 加载高清大图时内存管理

    IOS 多个UIImageView 加载高清大图时内存管理 时间:2014-08-27 10:47  浏览:59人 当我们在某一个View多个UIImageView,且UIImageView都显示的是 ...

  2. TableView_编辑 实例代码

    @interface MJViewController () <UITableViewDataSource, UITableViewDelegate> { NSMutableArray * ...

  3. 上海招聘FPGA讲师(专兼职均可)

    上海招聘FPGA讲师(专兼职均可) 招聘FPGA培训讲师(专兼职均可) 诚聘FPGA培训讲师_软件无线电方向 ◆精通FPGA设计方法及技巧 ◆精通无线电知识及System View设计方法,有基于Sy ...

  4. LED驅動芯片 兩種恒流控制方式

    下面要說的是,兩種恒流控制模式的開關電源,從而產生兩種做法.這兩種做法無論是原理,還是器件應用,還是性能差別,相當都較大.     首先說原理.第一種以現在恒流型LED專用IC為代表,主要如9910系 ...

  5. POJ1008 1013 1207 2105 2499(全部水题)

    做了一天水题,挑几个还算凑合的发上来. POJ1008 Maya Calendar 分析: #include <iostream> #include <cstdio> #inc ...

  6. 下载doxygen

    官网首页:http://www.stack.nl/~dimitri/doxygen/index.html 下载页面:http://www.stack.nl/~dimitri/doxygen/downl ...

  7. wxWidgets搜索事件处理函数顺序

    详细参见:使用wxWidgets进行跨平台程序开发 (王强 译) 实例: class MyFrame:public wxFrame { public: MyFrame() { wxButton* bu ...

  8. HDU 4604 Deque 最长子序列

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4604 Deque Time Limit: 4000/2000 MS (Java/Others)     ...

  9. C - Courses - hdu 1083(模板)

    一共有N个学生跟P门课程,一个学生可以任意选一 门或多门课,问是否达成: 1.每个学生选的都是不同的课(即不能有两个学生选同一门课) 2.每门课都有一个代表(即P门课都被成功选过) 输入为: P N( ...

  10. eXtremeDB

    I am doing a cluster test, why did I encounter the ld errorwhen compiling the several packages from ...