POJ 2492 并查集扩展(判断同性恋问题)
Time Limit:10000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
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
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
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
3 4
Sample Output
Scenario #1:
Suspicious bugs found! Scenario #2:
No suspicious bugs found!
Hint
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int father[maxn];
int other[maxn];
int rank[maxn];
int m,n; void init(){
for(int i=;i<=n;i++){
father[i]=i;
rank[i]=;
other[i]=;
}
} int get_father(int x){
if(x!=father[x])
father[x]=get_father(father[x]);
return father[x];
} void Union(int x,int y){
int t1=get_father(x);
int t2=get_father(y);
if(rank[t1]>rank[t2])
father[t2]=t1;
else{
father[t1]=t2;
if(rank[t1]==rank[t2])
rank[t2]++;
}
} int main(){
int t;
scanf("%d",&t);
int cnt=;
while(t--){
bool flag=false;
scanf("%d%d",&n,&m);
init();
int x,y;
for(int i=;i<=m;i++){
scanf("%d%d",&x,&y);
int t1=get_father(x);
int t2=get_father(y);
if(flag)
continue;
if(t1==t2){
flag=true;
continue;
}
if(other[x]){
Union(other[x],y);
}
else
other[x]=y;
if(other[y])
Union(other[y],x);
else
other[y]=x; } printf("Scenario #%d:\n",cnt++);
if(flag) printf("Suspicious bugs found!\n");
else
printf("No suspicious bugs found!\n");
puts("");
}
return ;
}
POJ 2492 并查集扩展(判断同性恋问题)的更多相关文章
- POJ 2492 并查集应用的扩展
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28651 Accepted: 9331 Descri ...
- poj 2492 并查集
思路:当a,b的根节点find(a)与find(b)不同时,就直接将这两个数连接起来.由于每个树的根节点的kind值一定为0,所以,对于a,b的kind值相同,我们就讲其中一个根的kind值变为1,当 ...
- POJ 2492 并查集 A Bug's Life
#include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> ...
- poj 1984 并查集
题目意思是一个图中,只有上下左右四个方向的边.给出这样的一些边, 求任意指定的2个节点之间的距离. 就是看不懂,怎么破 /* POJ 1984 并查集 */ #include <stdio.h& ...
- POJ 1182 (经典食物链 /并查集扩展)
(參考他人资料) 向量偏移--由"食物链"引发的总结 http://poj.org/problem?id=1182这道食物链题目是并查集的变型.非常久曾经做的一次是水过的,这次 ...
- POJ 3228 [并查集]
题目链接:[http://poj.org/problem?id=3228] 题意:给出n个村庄,每个村庄有金矿和仓库,然后给出m条边连接着这个村子.问题是把所有的金矿都移动到仓库里所要经过的路径的最大 ...
- poj 1733 并查集+hashmap
题意:题目:有一个长度 已知的01串,给出多个条件,[l,r]这个区间中1的个数是奇数还是偶数,问前几个是正确的,没有矛盾 链接:点我 解题思路:hash离散化+并查集 首先我们不考虑离散化:s[x] ...
- poj 3310(并查集判环,图的连通性,树上最长直径路径标记)
题目链接:http://poj.org/problem?id=3310 思路:首先是判断图的连通性,以及是否有环存在,这里我们可以用并查集判断,然后就是找2次dfs找树上最长直径了,并且对树上最长直径 ...
- poj 1797(并查集)
http://poj.org/problem?id=1797 题意:就是从第一个城市运货到第n个城市,最多可以一次运多少货. 输入的意思分别为从哪个城市到哪个城市,以及这条路最多可以运多少货物. 思路 ...
随机推荐
- jQuery使用之(四)处理页面的表单元素
表单是一个特殊的页面元素,value的值是最受关注的,jQuery提供了强大的val()方法来处理相关的操作. 1.获取表单元素的值. 直接调用val()方法时可以获取选择器的 中的第一个元素的val ...
- WPF MenuItem 四种角色分析
Menu Menu的样式很简单,就是顶部的那个框,如下图 而其中的文字“文件”“图形”...是属于MenuItem的,要灵活使用MenuItem,就需要了解MenuItem.Role的作用 T ...
- 调研eclipse安卓平台的开发环境
首先,我想抒发一下自己的感想.真的没想到这第一次的作业会这样的一波三折,本来以为自己已经弄好了eclipse,也弄过Java,安卓的环境配置应该不在话下,所以一拖再拖,从17号,也就是昨天开始,才着手 ...
- html之给文本框设置宽度和高度/input的无边框效果
<input name="" type="text" style="width:200px; height:20px;" /> ...
- 大数相乘算法C++版
#include <iostream> #include <cstring> using namespace std; #define null 0 #define MAXN ...
- JS_工厂模式
<!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...
- 【HDU 4925】BUPT 2015 newbie practice #2 div2-C-HDU 4925 Apple Tree
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/C Description I’ve bought an or ...
- Oracle11g 32位安装步骤
1.注意Oracle分成两个文件,下载完后,将两个文件解压到同一目录下即可. 路径名称中,最好不要出现中文,也不要出现空格等不规则字符.官方下地址:http://www.oracle.com/tech ...
- codeforces 719B:Anatoly and Cockroaches
Description Anatoly lives in the university dorm as many other students do. As you know, cockroaches ...
- poj 1743 二分答案+后缀数组 求不重叠的最长重复子串
题意:给出一串序列,求最长的theme长度 (theme:完全重叠的子序列,如1 2 3和1 2 3 or 子序列中每个元素对应的差相等,如1 2 3和7 8 9) 要是没有差相等这个条件那就好办 ...