POJ 2492 A Bug's Life 题解
这道题是一道标准的种类并查集:
种类并查集是给每个结点一个权值。然后在合并和查找的时候根据情况对权值来进行维护。
通过将原有的区间范围变大使并查集可以维护种类的联系:
#include <iostream>
#include <cstdio>
using namespace std;
int f[];
int zhaobaba(int x)
{
if(f[x]==x) return x;
return f[x]=zhaobaba(f[x]);
}
int main()
{
int T;
cin>>T;
int t=T;
while(t--){
int n,m;
cin>>n>>m;
for(int i=;i<=*n;i++) f[i]=i;
bool lala=;
for(int i=;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
if(lala) continue;
if(zhaobaba(u)==zhaobaba(v)){
lala=;
continue;
}
f[zhaobaba(u)]=zhaobaba(v+n);
f[zhaobaba(u+n)]=zhaobaba(v);
}
cout<<"Scenario #"<<T-t<<":"<<endl;
if(!lala){
cout<<"No suspicious bugs found!"<<endl;
}
else{
cout<<"Suspicious bugs found!"<<endl;
}
cout<<endl;
}
}
POJ 2492 A Bug's Life 题解的更多相关文章
- 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 A Bug's Life,二分染色。
Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs ...
- POJ 2492 A Bug's Life (并查集)
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...
- POJ 2492 A Bug's Life(带权并查集)
题目链接:http://poj.org/problem?id=2492 题目大意:有n只虫子,m对关系,m行每行有x y两个编号的虫子,告诉你每对x和y都为异性,先说的是对的,如果后面给出关系与前面的 ...
- POJ 2492 A Bug's Life
传送门:A Bug's Life Description Background Professor Hopper is researching the sexual behavior of a rar ...
- POJ 2492 A Bug's Life (并查集)
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 30130 Accepted: 9869 De ...
- POJ 2492 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 ...
随机推荐
- 1、Qt应用程序
新建Qt Widgets Application,基类选择QWidget Qt项目特点(参考上图):头文件名与类名一样,成对出现 main.cpp代码解释如下 #include "mywid ...
- Java类路径的问题
下面是eclipse中的文件组织形式. 下面是硬盘中文件的组织形式: src:中就是自己编写的没有编译的代码. target中是编译的Java中的class文件和一些不用编译的文件.这样也就明白了为什 ...
- Codeforces 1213D Equalizing by Division
cf题面 中文题意 给n个数,每次可以把其中一个数字位运算右移一位(即整除以二),问要至少操作几次才能让这n个数中有至少k个相等. 解题思路 这题还有个数据范围更小的简单版本,n和k是50,\(a_i ...
- 使用word2vec对中文维基百科数据进行处理
一.下载中文维基百科数据https://dumps.wikimedia.org/zhwiki/并使用gensim中的wikicorpus解析提取xml中的内容 二.利用opencc繁体转简体 三.利用 ...
- reactjs 的 css 模块化工具 styled-components 升级后 createGlobalStyle 废除,使用 createGlobalStyle 的方案
在 styled-components 升级到 4 版本后设置全局属性的 createGlobalStyle 这个 api 被废除,替代的 api 是 createGlobalStyle 与过去组织代 ...
- rem等比例自适应手机尺寸
方法:用sass的函数动态计算rem值 $rem : 75px;基准值 设计图是750的宽 设为$rem变量设为75,设计图是350的宽 设为$rem变量设为35,老的写法 需要用js来配合来动态改变 ...
- R_Studio(关联)对Groceries数据集进行关联分析
RGui的arules程序包里含有Groceries数据集,该数据集是某个杂货店一个月真实的交易记录,共有9835条消费记录,169个商品 #install.packages("arules ...
- 解决zbx的web界面zabbix服务器端运行中 显示为不(启动命令)
zabbix装完,发现server和agent服务都起来了,端口监听了,但是web界面zabbix服务器端运行中为 不 解决: 打开浏览器,到zabbix的setup.php界面 一般输入 ip/za ...
- DVWA--File Inclusion(不能远程包含的问题解决)
然后别以为这样就完了 我被这样坑了一下午 找到你对应版本的php 进去Ctrl+f 搜索url_allow——fopen 和include
- 分布式-信息方式-ActiveMQ静态网络连接多线程的consumer(消费者)访问集群
操作如下: 1:把整个conf文件夹复制一份,比如叫做conf22:修改里面的 activemq.xml文件(1)里面的 brokerName不能跟原来的重复(2)数据存放的文件名称不能重复,比如:& ...