hdu1829 A Bug's Life(并查集)
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX=2000;
int pre[2*MAX+5];
bool mark;
void init(int n){
int i;
for(i=1;i<=MAX+n;i++) pre[i]=i;
mark=true;
}
int root(int x){
if(x!=pre[x]){
pre[x]=root(pre[x]);
}
return pre[x];
}
void merge(int x,int y){
int fx,fy;
fx=root(x);
fy=root(y-MAX);
if(fx==fy) {
mark=false;
return;
}
fy=root(y);
if(fx!=fy){
pre[fx]=pre[fy];
}
}
int main()
{
int t,i,n,m,x,y,k;
scanf("%d",&t);
for(i=1;i<=t;i++){
scanf("%d%d",&n,&m);
init(n);
for(k=1;k<=m;k++){
scanf("%d%d",&x,&y);
if(mark){
merge(x,y+MAX);
merge(y,x+MAX);
}
}
printf("Scenario #%d:\n",i);
if(mark){
printf("No suspicious bugs found!\n");
}else{
printf("Suspicious bugs found!\n");
}
printf("\n");
}
return 0;
}
hdu1829 A Bug's Life(并查集)的更多相关文章
- HDU-1829 A Bug's Life。并查集构造,与POJ1709异曲同工!
A Bug's Life Find them, Catch them 都是并查集构造的题,不久前 ...
- poj2492--A Bug's Life(并查集变形)
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28703 Accepted: 9350 De ...
- 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 ——种类并查集
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28211 Accepted: 9177 De ...
- POJ 2492 A Bug's Life (并查集)
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...
- poj2492 A Bug's Life【并查集】
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assume ...
- A Bug's Life(加权并查集)
Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs ...
- POJ2492 A Bug's Life —— 种类并查集
题目链接:http://poj.org/problem?id=2492 A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Su ...
- POJ_2492 A Bug's Life 【并查集】
一.题面 POJ2492 二.分析 并查集判断类别的题目感觉套路都差不多. 还是先判断在不在一个集合里,在一个集合里才能判断是否同类. 若不在一个集合里则需要将这两个点联系起来. 关于联系起来后关系的 ...
随机推荐
- UITextField 点击事件 --- 不会触发键盘弹出,触发其他事件的实现。
今天在做项目的过程中,其中有三个控件: UITextField, UITextView , UILabel, 后来发现个问题:如果什么数据都不回填给textField.text 和 textView. ...
- 一个对象toString()方法如果没有被重写,那么默认调用它的父类Object的toString()方法,而Object的toString()方法是打印该对象的hashCode,一般hashCode就是此对象的内存地址
昨天因为要从JFrame控件获取密码,注意到一个问题,那就是用toString方法得到的不一定是你想要的,如下: jPasswordField是JFrame中的密码输入框,如果用下面的方法是得不到密码 ...
- BZOJ 2431 逆序对数列 DP
2431: [HAOI2009]逆序对数列 Time Limit: 5 Sec Memory Limit: 128 MB Description 对于一个数列{ai},如果有i< j且ai> ...
- B - Bit++
Problem description The classic programming language of Bitland is Bit++. This language is so peculi ...
- @PathVariable注解的使用和@Requestparam
一. @PathVariable @PathVariable这是一个路径映射格式的书写方式注解,在类映射路径的后加上/{对应方法参数中属性@PathVariable("code") ...
- 【SQL】联合语句
一.UNION操作符 UNION 操作符用于合并两个结果集,在合并的同时去掉重复行,并按合并后结果的第一列升序排列.合并后结果集的列名由第一个结果集的列名确定. UINON连接的两个结果集必须具有相同 ...
- ubuntu 14.04安装x11VNC
环境:Ubuntu 14.04, 1)安装x11vnc: sudo apt-get install x11vnc 2)设置VNC的连接密码: x11vnc -storepasswd Enter VNC ...
- 阿里巴巴矢量库IconFont__使用小录
使用阿里巴巴矢量库方法虽然不难,但本人记性不好,遂在次记录几笔 阿里巴巴矢量库地址:http://www.iconfont.cn/plus 阿里巴巴矢量库图标好处: 1:图标矢量化 2:自己总结:ic ...
- Mac下php连接mysql数据库失败解决办法
通过phpmyadmin连接mysql成功,但是通过php连接数据库失败,执行如下php语句 ? 1 @mysql_connect("localhost","root&q ...
- EF CodeFirst 基础命令
PM> enable-migrations 已在项目"EasyWeChat.Data"中启用迁移.若要覆盖现有迁移配置,请使用 -Force 参数. PM> add-m ...