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

#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. ES6 arrow function

    语法: () => { … } // 零个参数用 () 表示: x => { … } // 一个参数可以省略 (): (x, y) => { … } // 多参数不能省略 (): 当 ...

  2. android指纹识别、拼图游戏、仿MIUI长截屏、bilibili最美创意等源码

    Android精选源码 一个动画效果的播放控件,播放,暂停,停止之间的动画 用 RxJava 实现 Android 指纹识别代码 Android仿滴滴打车(滴滴UI)源码 Android高仿哔哩哔哩动 ...

  3. GitHub上fork别人打代码后如何保持和原作者同步的更新

    1.进入你的GitHub发起Pull  request 2.选择compare across  forks 3.反向操作.base fork改为自己的,head fork改为原作者的 4.点击 cre ...

  4. (转)基于MVC4+EasyUI的Web开发框架经验总结(12)--利用Jquery处理数据交互的几种方式

    http://www.cnblogs.com/wuhuacong/p/4085682.html 在基于MVC4+EasyUI的Web开发框架里面,大量采用了Jquery的方法,对数据进行请求或者提交, ...

  5. 题解 P3258 【[JLOI2014]松鼠的新家】(From luoguBlog)

    唯一能得分的题也被自己搞炸了,好的. 考场上读完题基本认定和lca脱不了干系,想了一会确认是树剖. 那么问题来了,考前一节课刚发现自己之前打的树剖是错的. 而且就算是错的我也没信心考场调出来. 于是打 ...

  6. 怎么从传统的盒子思想转为Flex 布局(css)

    前端进化很快,总是有新的技术出来,开始可能有些人用惯了盒子模型的思想 依赖 display属性 + position属性 + float属性.这三大件.它对于那些特殊布局非常不方便 我们就来看看Fle ...

  7. apicloud 第三方登录授权、微信、扣扣、微博登录授权

    授权登录.接入第三方的配置 例如:微信的登录授权. 首先在模块里面添加 wx 这个模块,然后在项目的配置文件里面进行配置. 配置的时候要现在微信开放平台 https://open.weixin.qq. ...

  8. Appium的ios配置

    automationName  text        XCUITest platformName             text          iOS platformVersion      ...

  9. 【剑指Offer】36、两个链表的第一个公共结点

      题目描述:   输入两个链表,找出它们的第一个公共结点.   解题思路:   本题首先可以很直观的想到蛮力法,即对链表1(假设长度为m)的每一个结点,遍历链表2(假设长度为n),找有没有与其相同的 ...

  10. 关于node对文件的读取

    设计: 通过终端git / cmd 获取用户输入路径,然后遍历路径下所有的文件,打印输出. 因为需要命令行交互,所以引入prompt库 (https://github.com/flatiron/pro ...