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个城市,最多可以一次运多少货. 输入的意思分别为从哪个城市到哪个城市,以及这条路最多可以运多少货物. 思路 ...
随机推荐
- webstrom 中启用emmet插件的方法
参考页面:https://www.jetbrains.com/help/webstorm/2016.2/enabling-emmet-support.html Basics Native Emmet ...
- 腾讯云CentOS7安装LNMP+wordpress
许多云主机都有学生优惠,于是我趁着现在大一买了个腾讯1元云主机+免费cn域名(高中生的话就别想了).鉴于我只知道用服务器安装博客,别的用途不了解,所以我就去安装wordpress. 而由于我看的教程有 ...
- Informatica 错误:Cannot convert from SQL type 93 to C type 4
经验和积累蛮重要!向大神学习! ---------------------------------------------------------------------- Mapping: 在sou ...
- SpringMVC数据库链接池,以及其他相关配置
1.applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...
- shopex商城的部署和安装
1.在网站上下载最新的压缩包: 2.shopex是商业软件,不开源,且源代码是加密的! 3.如果你出现zend的错误,是因为你的php环境没有安装此插件,推荐使用phpstudy最新版本,我使用的ph ...
- CodeReview Learning
目录 . 引言 . 代码检视的指导思想 . 代码检视的内容 . 回归测试 0. 引言 代码检视(Code Review)是指软件开发人员在完成代码设计.编写.调试后展开的个人或群体性的代码阅读过程,代 ...
- groovy-输入输出
Groovy为I/O提供了一系列的helper methods ,所有的这些方法都适用于标准的 Java Reader/Writer ,InputStream/OutputStream 和File 以 ...
- LeetCode 65 Valid Number
(在队友怂恿下写了LeetCode上的一个水题) 传送门 Validate if a given string is numeric. Some examples: "0" =&g ...
- MyEclipse2014中SVN的使用方法
MyEclipse中的SVN操作手册 1.导入项目 点击工具栏上的[File-Import],进入下图 (如果你的对话框中没有SVN这一条目,可能是因为你没有安装SVN插件,请安装完成后,在看这篇博客 ...
- 代码重构-3 用Tuple代替 out与ref
返回单一值是良好的编程习惯 原代码: public LotteryViewModel ValidateLottery(LotteryBaseData baseData, int authTime, o ...