2017ecjtu-summer training # 11 POJ 2492
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 38280 | Accepted: 12452 |
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
Output
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的更多相关文章
- 2017ecjtu-summer training #11 POJ 1018
Communication System Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 29218 Accepted: ...
- POJ 2492 并查集应用的扩展
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28651 Accepted: 9331 Descri ...
- POJ 2492 并查集扩展(判断同性恋问题)
G - A Bug's Life Time Limit:10000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u S ...
- A Bug's Life POJ - 2492 (带权并查集)
A Bug's Life POJ - 2492 Background Professor Hopper is researching the sexual behavior of a rare spe ...
- 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条臭 ...
- POJ 2492 A Bug's Life(并查集)
http://poj.org/problem?id=2492 题意 :就是给你n条虫子,m对关系,每一对关系的双方都是异性的,让你找出有没有是同性恋的. 思路 :这个题跟POJ1703其实差不多,也是 ...
- poj 2492(关系并查集) 同性恋
题目;http://poj.org/problem?id=2492 卧槽很前卫的题意啊,感觉节操都碎了, t组测试数据,然后n,m,n条虫子,然后m行,每行两个数代表a和b有性行为(默认既然能这样就代 ...
- (并查集)A Bug's Life -- POJ -- 2492
链接: http://poj.org/problem?id=2492 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#probl ...
- POJ 2492 A Bug's Life(带权并查集)
题目链接:http://poj.org/problem?id=2492 题目大意:有n只虫子,m对关系,m行每行有x y两个编号的虫子,告诉你每对x和y都为异性,先说的是对的,如果后面给出关系与前面的 ...
随机推荐
- bzoj 4569: [Scoi2016]萌萌哒
Description 一个长度为n的大数,用S1S2S3...Sn表示,其中Si表示数的第i位,S1是数的最高位,告诉你一些限制条件,每个条 件表示为四个数,l1,r1,l2,r2,即两个长度相同的 ...
- bzoj 4199: [Noi2015]品酒大会
Description 一年一度的"幻影阁夏日品酒大会"隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发"首席品酒家"和"首席猎手&quo ...
- 在Eclipse中查看JDK源码的方法
遇到这个问题是因为想证明Vector中的get()方法是不是用synchronized关键字修饰的来保证线程安全的方法, 于是在Eclipse里面: Vector<Integer> vec ...
- 在File Explorer的当前路径上直接打开VS Code
在C:\windows\system32文件下创建code.bat文件, 内容如下: @echo offstart "" "%ProgramFiles(x86)%\Mic ...
- 在macOS上通过pyenv安装和切换多版本Python
1. 安装homebrew 官网 http://brew.sh/index_zh-cn.html 打开终端,在终端中粘贴如下脚本 /usr/bin/ruby -e "$(curl -fsSL ...
- GpG使用指南
1. 简介 1991年,程序员Phil Zimmermann为了避开政府监视,开发了加密软件PGP.这个软件非常好用,迅速流传开来,成了许多程序员的必备工具.但是,它是商业软件,不能自由使用.所以,自 ...
- Ant Design Pro 学习二 新建菜单-布局
新建布局,注意格式: src/common/nav.js 中增加 { component: dynamicWrapper(app, [], () => import('/path/to/NewL ...
- Spring Boot实战:Restful API的构建
上一篇文章讲解了通过Spring boot与JdbcTemplate.JPA和MyBatis的集成,实现对数据库的访问.今天主要给大家分享一下如何通过Spring boot向前端返回数据. 在现在的开 ...
- React学习之路(二)
状态(state) 状态就是组件描述某种显示情况的数据,由组件自己设置和更改,也就是所组件自己维护,目的就是为了在不同状态下使组件的显示不同 在组件中只能通过getInitialState的钩子函数来 ...
- mysql优化-》查询缓存
使用MySql查询缓存(query_cache_size) 在MySql中查询缓存的原理: 其实是MySql创建了一个临时的空间叫Qcache(这个空间生成在MySql的编译器内存中),这个空间的大小 ...