hdoj--1829--A Bug's Life(带权并查集)
A Bug's Life
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.
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.
about the bugs' sexual behavior, or "Suspicious bugs found!" if Professor Hopper's assumption is definitely wrong.
2
3 3
1 2
2 3
1 3
4 2
1 2
3 4
Scenario #1:
Suspicious bugs found! Scenario #2:
No suspicious bugs found!HintHuge input,scanf is recommended.
//题意:给出了1--n的m种关系,x--y中x跟y的性别未知,问有没有可能出现同性恋
//一道带权并查集问题, 不要考虑x,y的性别是男是女,只需要考虑他们的性别
//是否一致, vis数组存放爱人的性别,
//举例子:x--y说明x和y有关系
//如果说vis[x]不为零说明x有了爱人,并且vis[x]存放的就是他爱人的性别,
//那么 那么y一定跟vis[x]是一个集合中的,不需要管vis存放的都是什么数字
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define MAXN 10000010
int n,m,flag;
int vis[MAXN],pre[MAXN];
void init()
{
for(int i=0;i<=n;i++)
{
vis[i]=0;
pre[i]=i;
}
}
int find(int x)
{
if(pre[x]!=x)
pre[x]=find(pre[x]);
return pre[x];
}
void join(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx==fy)
flag=1;
else
{
if(vis[fx])//如果说fx已经有了爱人,那么fy的性别一定要和fx性别一样
pre[vis[fx]]=fy;
if(vis[fy])
pre[vis[fy]]=fx;
vis[fx]=fy;vis[fy]=fx;//vis存放爱人的性别,fx爱人的性别是fy
//如果说两人性别不属于同一结合,两人可以相恋
}
}
int main()
{
int t,k=1;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();
flag=0;
int x,y;
for(int i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
join(x,y);
}
printf("Scenario #%d:\n",k++);
if(flag)
printf("Suspicious bugs found!\n\n");
else
printf("No suspicious bugs found!\n\n");
}
return 0;
}
hdoj--1829--A Bug's Life(带权并查集)的更多相关文章
- POJ2492 A Bug's Life 带权并查集
分析:所谓带权并查集,就是比朴素的并查集多了一个数组,记录一些东西,例如到根的距离,或者和根的关系等 这个题,权数组为relation 代表的关系 1 和父节点不同性别,0,和父节点同性别 并查集一 ...
- POJ 2492 A Bug's Life 带权并查集
题意: 思路: mod2 意义下的带权并查集 如果两只虫子是异性恋,它们的距离应该是1. 如果两只虫子相恋且距离为零,则它们是同性恋. (出题人好猥琐啊) 注意: 不能输入一半就break出来.... ...
- poj2492A Bug's Life——带权并查集
题目:http://poj.org/problem?id=2492 所有元素加入同一个并查集中,通过其偏移量%2将其分类为同性与异性,据此判断事件. 代码如下: #include<iostrea ...
- POJ 2492 A Bug's Life (带权并查集 && 向量偏移)
题意 : 给你 n 只虫且性别只有公母, 接下来给出 m 个关系, 这 m 个关系中都是代表这两只虫能够交配, 就是默认异性, 问你在给出的关系中有没有与异性交配这一事实相反的, 即同性之间给出了交配 ...
- HDU 1829 A Bug's Life 【带权并查集/补集法/向量法】
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...
- hdu 1829 &poj 2492 A Bug's Life(推断二分图、带权并查集)
A Bug's Life Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- hdu 1829-A Bug's LIfe(简单带权并查集)
题意:Bug有两种性别,异性之间才交往, 让你根据数据判断是否存在同性恋,输入有 t 组数据,每组数据给出bug数量n, 和关系数m, 以下m行给出相交往的一对Bug编号 a, b.只需要判断有没有, ...
- A Bug's Life POJ - 2492 (带权并查集)
A Bug's Life POJ - 2492 Background Professor Hopper is researching the sexual behavior of a rare spe ...
- poj2492 A Bug's Life(带权并查集)
题目链接 http://poj.org/problem?id=2492 题意 虫子有两种性别,有n只虫子,编号1~n,输入m组数据,每组数据包含a.b两只虫子,表示a.b为不同性别的虫子,根据输入的m ...
随机推荐
- 用 Django2.0 做 简单的BBS(前端用 Bootstrap)
实现目标: 开发首页显示BBS的标题和摘要,点击BBS的标题可跳转到BBS详细页面进行展示. 开发环境及开发工具: Python 3.6.3 Django 2.0 Pycharm 2017.3 实现过 ...
- Android MediaRecorder自定义分辨率
Android MediaRecorder自定义分辨率 工作这么久了,确实积累了不少东西,但都是以文档的形式存在U盘里的,为什么不写博客呢?因为懒啊!!!总感觉博客太难写了(大概是上学时候写作文恐惧症 ...
- Windows7 win10 系统如何强制禁用驱动程序签名
转载自奇兔 Win7 64位系统禁用驱动程序签名强制 Win7系统是比较稳定的一款系统,也是最多人在使用的一款系统.当我们在Win7系统中安装驱动程序的时候,对安装的驱动程序需要数字签名,否则驱 ...
- 基于神经网络的混合计算(DNC)-Hybrid computing using a NN with dynamic external memory
前言: DNC可以称为NTM的进一步发展,希望先看看这篇译文,关于NTM的译文:人工机器-NTM-Neutral Turing Machine 基于神经网络的混合计算 Hybrid computing ...
- 时序分析:串匹配—Brute-Force算法
在使用KMP算法之前,使用了BF算法用于串匹配:原文链接已无法查找..... 设有主串s和子串t,子串t的定位就是要在主串s中找到一个与子串t相等的子串.通常把主串s称为目标串,把子串t ...
- dispatch_sync
dispatch_sync does two things: queue a block blocks the current thread until the block has finished ...
- js-循环执行一个函数
js里的两个内置函数:setInterval()与setTimeout()提供了定时的功能,前者是每隔几秒执行一次,后者是延迟一段时间执行一次.javascript 是一个单线程环境,定时并不是很准, ...
- BZOJ 1740: [Usaco2005 mar]Yogurt factory 奶酪工厂 贪心 + 问题转化
Description The cows have purchased a yogurt factory that makes world-famous Yucky Yogurt. Over the ...
- Django 模型层(标签、过滤器、模板的继承与导入)
过滤器/自定义过滤器 模板语法中的过滤器类似于python中的内置方法,在我们把数据从后端通过rander传入到前端html文件中之后,在前端我们可以通过模板语法,对传入的数据再进行以通骚操作. 首先 ...
- eas之Uuid和BOSUuid 区别
BOSUuid 加入了BOSType的概念,这个唯一码跟 BOSType有关,里面包含了BOSType的信息. 根据BOSType可以生产BOSUuid,同样,根据BOSUuid也可以找到BOSTyp ...