A Bug's Life
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 38280   Accepted: 12452

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!
题目意思就是 给定n只虫子 不同性别的可以在一起 相同性别的不能在一起
给你m对虫子 判断中间有没有同性别在一起的;
我们把同性的放到一个集合里 如果一个集合里出现了异性 则说明存在同性恋在一起
假设 x 为一种性别 x+n为与其相反的性别  
若a,b为同性 的  我们则可以把判断 (a,b+n)  (b,a+n)为异性反之亦然;
AC代码
 #include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <queue>
#include <vector>
#include <algorithm>
#define maxn 2010
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
int par[maxn*];
void init(int n)
{
for(int i=;i<=n;i++)
{
par[i]=i;
}
}
int find(int x)
{
if(par[x]==x)
return x;
else
return par[x]=find(par[x]);
}
void uion(int x,int y)
{
int x1=find(x);
int y1=find(y);
if(x1!=y1)
par[x1]=y1;
return;
}
int main(int argc, char const *argv[])
{
int t;
cin>>t;
for(int i=;i<=t;i++)
{
int n,m;
cin>>n>>m;
init(n*);
int x,y;
int flag=;
for(int j=;j<m;j++)
{
scanf("%d %d",&x,&y);
if(find(x)==find(y)||find(x+n)==find(y+n))
{
flag=;
printf("Scenario #%d:\nSuspicious bugs found!\n\n",i);
break;
}
else
{
uion(x,y+n);
uion(x+n,y);
}
}
if(flag==)
printf("Scenario #%d:\nNo suspicious bugs found!\n\n",i);
}
return ;
}

看到某博客另一种写法很不错

http://www.cnblogs.com/dongsheng/archive/2012/08/08/2627917.html

 /* 功能Function Description:     POJ-2492 并查集应用的扩展
开发环境Environment: DEV C++ 4.9.9.1
技术特点Technique:
版本Version:
作者Author: 可笑痴狂
日期Date: 20120808
备注Notes:
关于并查集,注意两个概念:按秩合并、路径压缩。
1、按秩合并
由于并查集一般是用比较高效的树形结构来表示的,按秩合并的目的就是防止产生退化的树(也就是类似链表的树),
用一个数组记录各个元素的高度(也有的记录各个元素的孩子的数目,具体看哪种能给解题带来方便),
然后在合并的时候把高度小的嫁接到高度大的上面,从而防止产生退化的树。
2、路径压缩
而另一个数组记录各个元素的祖先,这样就防止一步步地递归查找父亲从而损失的时间。因为并查集只要搞清楚各个元素所在的集合,
而区分不同的集合我们用的是代表元素(也就是树根),所以对于每个元素我们只需保存其祖先,从而区分不同的集合。
而我们这道题并没有使用纯正的并查集算法,而是对其进行了扩展,
我们并没有使用“1、按秩合并”(当然你可以用,那样就需要再开一个数组)
我们从“1、按秩合并”得到启示,保存“秩”的数组保存的是 元素相对于父节点的关系 ,我们岂不可以利用这种关系
(即相对于父节点的不同秩值来区分不同的集合),从而可以把两个集合合并成一个集合。
(注:此代码 relation=0 代表 和父节点同一性别)
*/ #include<stdio.h>
int father[];
int relation[]; int find_father(int i)
{
int t;
if(father[i]==i)
return i; //计算相对于新的父节点(即根)的秩,relation[t]是老的父节点相对于新的父节点(即根)的秩,relation[i]是i元素相对于老的父节点的秩,
//类似于物理里的相对运动,得到的r[i]就是相对于新的父节点(即根)的秩。而且这个递归调用不会超过两层
t=father[i];
father[i]=find_father(father[i]);
relation[i]=(relation[i]+relation[t]+)%; //注意递归中把这棵树relation中的的值都更新一遍,这句的顺序 不能 和上一句 调换位置
// relation[a]的改变是伴随着father[a]的改变而更新的(有father改变就有relation改变),要是father改变了,而relation未改变,此时的relation就记录了一个错误的值,
//father未改变(即使实际的father已不是现在的值,但只要father未改变,relation的值就是“正确”的,认识到这点很重要。)
return father[i];
} void merge(int a,int b)
{
int x,y;
x=find_father(a);
y=find_father(b);
father[x]=y;
relation[x]=(relation[b]-relation[a])%;//relation[a]+relation[x]与relation[b]相对于新的父节点必须相差1个等级,因为他们不是gay
} //x下边的节点不用改,因为查找的时候会自动更新 int main()
{
int T,m,n,i,j,a,b,flag;
scanf("%d",&T);
for(i=;i<=T;++i)
{
flag=;
scanf("%d%d",&n,&m);
for(j=;j<=n;++j) //初始化
{
father[j]=j;
relation[j]=;
}
for(j=;j<=m;++j)
{
scanf("%d%d",&a,&b);
if(find_father(a)==find_father(b))
{
// if(relation[a]!=(relation[b]+1)%2)
if(relation[a]==relation[b]) //说明是同性
flag=;
}
else
merge(a,b);
}
if(flag)
printf("Scenario #%d:\nSuspicious bugs found!\n\n",i);
else
printf("Scenario #%d:\nNo suspicious bugs found!\n\n",i);
}
return ;
}

2017ecjtu-summer training # 11 POJ 2492的更多相关文章

  1. 2017ecjtu-summer training #11 POJ 1018

    Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29218   Accepted:  ...

  2. POJ 2492 并查集应用的扩展

    A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28651 Accepted: 9331 Descri ...

  3. POJ 2492 并查集扩展(判断同性恋问题)

    G - A Bug's Life Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

  4. A Bug's Life POJ - 2492 (带权并查集)

    A Bug's Life POJ - 2492 Background Professor Hopper is researching the sexual behavior of a rare spe ...

  5. 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条臭 ...

  6. POJ 2492 A Bug's Life(并查集)

    http://poj.org/problem?id=2492 题意 :就是给你n条虫子,m对关系,每一对关系的双方都是异性的,让你找出有没有是同性恋的. 思路 :这个题跟POJ1703其实差不多,也是 ...

  7. poj 2492(关系并查集) 同性恋

    题目;http://poj.org/problem?id=2492 卧槽很前卫的题意啊,感觉节操都碎了, t组测试数据,然后n,m,n条虫子,然后m行,每行两个数代表a和b有性行为(默认既然能这样就代 ...

  8. (并查集)A Bug's Life -- POJ -- 2492

    链接: http://poj.org/problem?id=2492 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#probl ...

  9. POJ 2492 A Bug's Life(带权并查集)

    题目链接:http://poj.org/problem?id=2492 题目大意:有n只虫子,m对关系,m行每行有x y两个编号的虫子,告诉你每对x和y都为异性,先说的是对的,如果后面给出关系与前面的 ...

随机推荐

  1. kafka副本机制之数据可靠性

    一.概述 为了提升集群的HA,Kafka从0.8版本开始引入了副本(Replica)机制,增加副本机制后,每个副本可以有多个副本,针对每个分区,都会从副本集(Assigned Replica,AR)中 ...

  2. Java 包装类Integer的值比较

    对于包装类型Integer的值比较与int的值比较是不同的:   public class Java_Val_Compare { public static void main(String[] ar ...

  3. (转)Linux安装Tomcat

    为了学习java,需要一个服务器,因此决定用比较流行的tomcat.根据网上对安装tomcat的介绍,自己进行了安装,现在已经成功了,现在把安装的过程进行记录,也供大家学习参考.   一.从官方网站上 ...

  4. DevOps/TestOps概念

    天下大势分久必合合久必分,早期的软件开发只有软件工程师一人完成,为了提高效率逐渐实行分工模式:开发.测试.运维.不同角色担任不同的任务.分工越来越细之后带来了问题也越来越突出,那就是各角色之间的沟通成 ...

  5. 记一次Hbase查询速度优化经历

    项目背景: 在这次影像系统中,我们利用大数据平台做的是文件(图片.视频等)批次的增删改查,每个批次都包含多个文件,上传完成以后要添加文件索引(文件信息及批次信息),由于在Hbase存储的过程中,每个文 ...

  6. linux socket编程:简易客户端与服务端

    什么是socket? socket起源于Unix,而Unix/Linux基本哲学之一就是“一切皆文件”,都可以用“打开open –> 读写write/read –> 关闭close”模式来 ...

  7. Python的Django框架完成一个完整的论坛(源码以及思路)

    一个完整的论坛,登录.注册.发表.头像.点赞.评论.分页.阅读排行等 使用Django2,Python3.5 开发工具:Pycharm5 需要的知识:Python基础知识,Django原理的理解以及使 ...

  8. AI时代:推荐引擎正在塑造人类

    We shape our tools and afterwards our tools shape us. ------Marshall McLuhan 麦克卢汉说:"我们塑造了工具,反过来 ...

  9. Android App版本自动更新

    App在开发过程中,随着业务场景的不断增多,功能的不断完善,早期下载App的用户便无法体验最新的功能,为了能让用户更及时的体验App最新版本,在App开发过程加入App自动更新功能便显得尤为重要.更新 ...

  10. 高通msm8909耳机调试

    http://blog.csdn.net/mike8825/article/details/69489865?locationnum=3&fps=1 1.DTS相应修改: DTS相关代码:ke ...