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都为异性,先说的是对的,如果后面给出关系与前面的 ...
随机推荐
- React:入门计数器
---恢复内容开始--- 把React的官网入门例子全看一遍,理解了,但自己从头开始写有点困难,这次强迫自己从头开始写,并写好注释: import React, { Component } from ...
- linux系统下phpstudy里的mysql使用方法
linux作为一个优秀的服务器端管理系统,其实linux的桌面系统也用起来十分的nice.好吧,如何你在做开发的时候在linux下安装了lmap或者phpstudy,那么在第一次使用其自带的mysql ...
- ADO.NET查询和操作数据库
stringbuilder 类 stringbuilder类:用来定义可变字符串 stringbulider Append(string value) 在结尾追加 stringbuilder in ...
- 前端之JavaScript--基础
JavaScript 独立的语言,浏览器具有js解释器 一. JavaScript代码存在形式: - Head中 <script> //javascript代码 alert(123); & ...
- python实现散列表的链表法
在散列中,链接法是一种最简单的碰撞解决技术,这种方法的原理就是把散列到同一槽中的所有元素 都放在一个链表中. 链接法有两个定理,定理一: 在简单一致散列的假设下,一次不成功查找的期望时间为O(1 + ...
- Layout 不可思议(一)—— CSS 实现自适应的正方形卡片
最近被一个布局问题给难住了,枉我一向自称掌握最好的前端技能是 CSS,写完博客就得敷脸去 需求是实现一个自适应的正方形卡片,效果如下: 顺便(开个坑)写个系列,总结那些设计精妙的布局结构 本次页面的 ...
- SQL SERVER 日期转换大全
博客转自:http://blog.csdn.net/baiduandxunlei/article/details/9180075 CONVERT(data_type,expression[,style ...
- 系统 TIME_WAIT累积与端口耗尽的问题
调整内核参数 net.ipv4.tcp_tw_reuse = net.ipv4.tcp_tw_recycle = 这两个参数可以让 tcp 连接回收.再利用. 摘录 『HTTP 权威指南』page ...
- Struts2内部执行过程
首先是Struts2的流程图. 一.当有一个请求的时候.执行以下流程. 1 客户端初始化一个指向Servlet容器的请求: 2 这个请求经过一系列的过滤器(Filter)(这些过滤器中有一个叫做Act ...
- 进程管理工具Supervisor(一)简介与使用
Supervisor是用Python开发的一套client/server架构的进程管理程序,能做到开机启动,以daemon进程的方式运行程序,并可以监控进程状态等等. linux进程管理方式有传统的r ...