开两个并查集。然后合并的时候要合并两次。这样在合并之前推断是否冲突,假设不冲突就进行合并,否则不须要继续合并。

#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&#39;s Life(并查集)的更多相关文章

  1. HDU-1829 A Bug's Life。并查集构造,与POJ1709异曲同工!

    A Bug's Life                                                     Find them, Catch them 都是并查集构造的题,不久前 ...

  2. poj2492--A Bug&#39;s Life(并查集变形)

    A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 28703   Accepted: 9350 De ...

  3. POJ 2492 A Bug's Life【并查集高级应用+类似食物链】

    Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...

  4. 【POJ】2492 A bug's life ——种类并查集

    A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 28211   Accepted: 9177 De ...

  5. POJ 2492 A Bug's Life (并查集)

    Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...

  6. poj2492 A Bug's Life【并查集】

    Background  Professor Hopper is researching the sexual behavior of a rare species of bugs. He assume ...

  7. A Bug's Life(加权并查集)

    Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs ...

  8. POJ2492 A Bug's Life —— 种类并查集

    题目链接:http://poj.org/problem?id=2492 A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Su ...

  9. POJ_2492 A Bug's Life 【并查集】

    一.题面 POJ2492 二.分析 并查集判断类别的题目感觉套路都差不多. 还是先判断在不在一个集合里,在一个集合里才能判断是否同类. 若不在一个集合里则需要将这两个点联系起来. 关于联系起来后关系的 ...

随机推荐

  1. iOS 点击事件传递及响应

    1.iOS中的事件 iOS中的事件可以分为3大类型: 触摸事件 加速计事件 远程控制事件这里我们只讨论iOS中的触摸事件. 1.1响应者对象(UIResponder) 在iOS中不是任何对象都能处理事 ...

  2. android 自定义view 前的基础知识

    本篇文章是自己自学自定义view前的准备,具体参考资料来自 Android LayoutInflater原理分析,带你一步步深入了解View(一) Android视图绘制流程完全解析,带你一步步深入了 ...

  3. B - Taxi(贪心)

    Problem description After the lessons n groups of schoolchildren went outside and decided to visit P ...

  4. Django html页面 'ascii' codec can't encode characters in position 8-10: ordinal not

    用Django开发的页面,之前用的是python3.X,后来又换成python2.X后各种报错,编码问题,于是在所有python文件开头加了编码:#coding=utf-8 但是后来发现,有些文件加了 ...

  5. Jquery 获取父页面下指定iframe里的指定元素

    var div1=$("#iframe1",window.parent.document).contents().find("#div1");

  6. IIS 7.0、IIS 7.5 和 IIS 8.0 使用的 HTTP 状态代码【转载自微软官方】

    HTTP 状态代码 本部分描述 IIS 7.0.IIS 7.5 和 IIS 8.0 使用的 HTTP 状态代码. 注意 本文不会列出 HTTP 规范中所述的每个可能的 HTTP 状态代码.本文只包括 ...

  7. Java中last_insert_id的使用

    last_insert_id的作用是:在当前表中的主键是自增时,插入一条新记录的同时使用last_insert_id可以获取当前的新记录的主键id. 下面是一个例子: import java.sql. ...

  8. CSS框架Bootstrap

    作为一个软件开发人员,经常接触和使用框架是再平常的事情不过了.但是这些框架基本都是和语言相关的,比如WEB框架SpringMVC,JavaEE框架Spring,ORM框架Hibernate,还有Jav ...

  9. openMSP430之openmsp430-loader

    openmsp430-loader This simple program allows the user to load the openMSP430 program memory with an ...

  10. centos开机运行级别更改

    1.使用命令切换运行级别/目标 # systemctl isolate multi-user.target //切换到运行级别3,该命令对下次启动无影响,等价于telinit 3 # systemct ...