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都为异性,先说的是对的,如果后面给出关系与前面的 ...
随机推荐
- DeepLearning.ai学习笔记(三)结构化机器学习项目--week1 机器学习策略
一.为什么是ML策略 如上图示,假如我们在构建一个喵咪分类器,数据集就是上面几个图,训练之后准确率达到90%.虽然看起来挺高的,但是这显然并不具一般性,因为数据集太少了.那么此时可以想到的ML策略有哪 ...
- ElasticSearch 学习记录之如任何设计可扩容的索引结构
扩容设计 扩容的单元 一个分片即一个 Lucene 索引 ,一个 Elasticsearch 索引即一系列分片的集合 一个分片即为 扩容的单元 . 一个最小的索引拥有一个分片. 一个只有一个分片的索引 ...
- 3、树莓派的配置:改静态IP、连接ssh、安装中文字体、安装谷歌输入法、增加USB电流、修改触摸屏分辨率、扩展sd卡空间、修复vi和vim乱码问题、安装配置远程桌面vnc
本博文仅作本人操作过程的记录,留作备忘.自强不息 QQ1222698 1.连接上HDMI线,插上触摸屏,插上键盘,鼠标,网线,启动.系统正常启动,但是一直闪烁,不停的黑屏,是由于触摸屏的usb口供电不 ...
- 我搞zabbix的那两天(2)
摘要:前一篇(我搞zabbix的那两天(1))我介绍了Zabbix的安装部署以及遇到的问题,这一篇将介绍zabbix 使用及短信等告警实现!!! Zabbix主界面及汉化方法介绍 1.1 初始化主界面 ...
- grpc介绍
grpc入门(一) 一.什么是grpc grpc是谷歌开源的一款高性能的rpc框架 (https://grpc.io),可以使用protocol buffers作为IDL(Interface Defi ...
- spring中使用Hibernate中的getCurrentSession报出:createQuery is not valid without active transaction
1.错误信息 HTTP Status 500 - createQuery is not valid without active transaction type Exception report m ...
- JS函数的参数声明中用 var 与不用 var的区别
1.var 声明的变量,作用域是当前 function 2.没有声明的变量,直接赋值的话, 会自动创建变量,但作用域是全局的. 例如: function doSth() { a = "AAA ...
- JS 实现图片的预加载(转载)
图片预加载是web开发中一种应用相当广泛的技术,比如我们在做图片翻转显示等特效的时候,为了让图片在转换的时候不出现等待,我们最好是先让图片下载到本地,然后在继续执行后续的操作. 下面的函数实现了一个我 ...
- angular4.0项目main.ts详解
main.ts负责引导整个angular应用的起点 // 导入enableProdMode用来关闭angular开发者模式 import { enableProdMode } from '@angul ...
- Nodejs的运行原理-调用篇
前言 之前做过Nodejs的架构篇, 有很多朋友留言给我,说没看懂里面的例子,这里我会重新梳理一下,再以http server为例,来解析Nodejs从前端到libuv的调用过程. 正文 回忆a. N ...