POJ 2492 并查集应用的扩展
A Bug's Life
Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 28651 Accepted: 9331
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!
Hint
Huge input,scanf is recommended.
Source
TUD Programming Contest 2005, Darmstadt, Germany
/*********************************
author : Grant Yuan
time : 2014/10/5 11:29
algorithm: 并查集应用的扩展
source : POJ 2492
**********************************/
#include<iostream>
#include<cstdio>
#define MAX 2007
using namespace std;
int parent[MAX],relation[MAX];
int n,t,m;
int find_parent(int k)
{
if(k==parent[k])
return k;
int t=parent[k];
parent[k]=find_parent(parent[k]);
relation[k]=(relation[k]+relation[t]+1)%2;
return parent[k];
}
void unite(int a,int b)
{
int pa=find_parent(a);
int pb=find_parent(b);
parent[pa]=pb;
relation[pa]=(relation[b]-relation[a])%2;
}
int main()
{
scanf("%d",&t);
int ct=1;bool first=0;
while(t--){
bool ans=0;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
parent[i]=i,relation[i]=1;
for(int i=1;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
int pa=find_parent(a);
int pb=find_parent(b);
if(pa==pb){
if(relation[a]==relation[b]) ans=1;
}
else unite(a,b);
}
printf("Scenario #%d:\n",ct++);
if(ans) printf("Suspicious bugs found!\n\n");
else printf("No suspicious bugs found!\n\n");
}
return 0;
}
POJ 2492 并查集应用的扩展的更多相关文章
- POJ 2492 并查集扩展(判断同性恋问题)
G - A Bug's Life Time Limit:10000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u S ...
- 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 1797(并查集)
http://poj.org/problem?id=1797 题意:就是从第一个城市运货到第n个城市,最多可以一次运多少货. 输入的意思分别为从哪个城市到哪个城市,以及这条路最多可以运多少货物. 思路 ...
- 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 3657 并查集
题意: 思路: 1.二分+线段树(但是会TLE 本地测没有任何问题,但是POJ上就是会挂--) 2.二分+并查集 我搞了一下午+一晚上才搞出来----..(多半时间是在查错) 首先 如果我们想知道这头 ...
随机推荐
- AspUpload组件的安装及使用方法介绍
http://soft.huweishen.com/soft/47.html AspUpload对ASP编程人员来说要实现ASP网站文件上传功能它是首选.本文就为大家介绍一下AspUpload组件的安 ...
- c++学籍管理系统v1.10
//////////////新增添加学生和成绩录入系统 #include<iostream> #include <string> #include<conio.h> ...
- Python学习笔记12:标准库之对象序列化(pickle包,cPickle包)
计算机的内存中存储的是二进制的序列. 我们能够直接将某个对象所相应位置的数据抓取下来,转换成文本流 (这个过程叫做serialize),然后将文本流存入到文件里. 因为Python在创建对象时,要參考 ...
- SVN命令汇总
1.将文件checkout到本地文件夹 svn checkout path(path是server上的文件夹) 比如:svn checkout svn://192.168.1.1/pro/doma ...
- 第十三章——表和索引分区(1)——使用Range Left进行表分区
原文:第十三章--表和索引分区(1)--使用Range Left进行表分区 前言: 如果数据表的数据持续增长,并且表中的数据量已经达到数十亿甚至更多,数据的查询和操作将非常困难,面对非常庞大的表,几时 ...
- Maven+Spring
Maven+Spring 关于Maven Maven是一个用于项目构建的工具,通过它便捷的管理项目的生命周期.即项目的jar包依赖,开发,测试,发布打包. 做过.NET的人应该会联想到Nuget,是的 ...
- IT该忍者神龟Jquery小工具easyUI物业摘要召回
找了个时间看了下EasyUI插件.对它的插件感觉是非常舒服,特地把Easy UI的大部分功能属性做了一下汇总. 此属性列表请对比jQuery EasyUI 1.0.5,关于它的很多其它资讯请猛击这里. ...
- “采用VS2010至MFC4.2发育”最后溶液
我层2010年这方面的研究进行了简单(http://blog.csdn.net/boweirrking/article/details/5477062),那时候没有深入思考过这当中的原理,最终给出的方 ...
- HDU——B-number(数字DP)
标题效果: 要了解1至n如何号码之间有许多含有13,并13可分 记忆化搜索: dp[pos][pre][mod][statu],pos位数,pre前一位,mod余数,statu状态 有2个状态:含13 ...
- DirectX 11游戏编程学习笔记2: 文章1章Vector Algebra(向量代数)
本文由哈里_蜘蛛侠原创,转载请注明出处.有问题欢迎联系2024958085@qq.com 注:我给的电子版是700多页.而实体书是800多页,所以我在提到相关概念的时候.会使用章节号而 ...