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 ...
随机推荐
- python基础--几个特性
1.helloword程序的解释 #!/usr/bin/python3 print("Hello, World!") 关于脚本第一行的 #!/usr/bin/python 的解释, ...
- AbpUser 扩展
AbpUser表存放的信息比较少,现扩展一下信息 1.在Core层添加UserExtend 类,继承 AbpUser<User>,写入以上各项属性字段,并添加Discriminator 字 ...
- Linux压缩工具
一.gzip/gunzip/zcat gzip, gunzip, zcat - compress or expand files gzip [ option .... ] [ filenames .. ...
- 桥接模式下,主机能ping通虚拟机,虚拟机ping不通主机
好像是防火墙阻止了什么东西而导致的无法ping通! 1.打开WIN7防火墙 2.选择高级设置 3.入站规则 4.找到配置文件类型为“公用”的“文件和打印共享(回显请求 – ICMPv4-In)”规则, ...
- wordpress 更新时需要FTP 服务器账户密码的解决方法
首先进入 apache下的wordpress 的目录下 (这是apache服务器默认安装的路径) cd /var/www 接着,给html整个文件夹进行赋值权限,如果不赋予权限 更新的时候会报权限不足 ...
- leetcode 116填充每个节点的下一个右侧节点指针
time O(n) ,sapce O(n) /* // Definition for a Node. class Node { public: int val; Node* left; Node* r ...
- leetcode 51 N皇后问题
代码,由全排列转化而来,加上剪枝,整洁的代码: 共有4个变量,res(最终的结果),level,当前合理的解,n皇后的个数,visit,当前列是否放过皇后,由于本来就是在新的行方皇后,又通过visit ...
- 浏览器端-W3School-Browser:Window 对象
ylbtech-浏览器端-W3School-Browser:Window 对象 1.返回顶部 1. Window 对象 Window 对象 Window 对象表示浏览器中打开的窗口. 如果文档包含框架 ...
- idea -- spring datasource配置文件不显示datasource.properties文件对应属性的值,错误提示cannot resolve property key
原文:https://yq.aliyun.com/articles/657711 点击 文件 顶部的 蓝色 MVC application context,修改为Local File
- Django测试环境环境配置
安装Django 在cmd的管理者模式下进入Python的安装目录 敲入命令:pip install Django -看到如下的信息表示成功 如果你在运行其它人写的Django系统时出现以下错误信息, ...