题意:有n只虫子,每次给出一对互为异性的虫子的编号,输出是否存在冲突。

思路:用并查集,每次输入一对虫子后就先判定一下。如果两者父亲相同,则说明关系已确定,再看性别是否相同,如果相同则有冲突。否则就将两只虫子并入一个集合。

而性别则是用一个gender数组来维护,每个虫子的gender的值为0或者1。

0表示该虫子的性别与父亲结点的性别相同。

1表示该虫子的性别与父亲结点的性别不同。

这题和poj1703本质上是一样的。1703的题解请点传送门

另外还有一个疑惑,本题输入量巨大,我用输入加速后反而比用scanf要慢得多,弄不明白为什么。。有知道的大神欢迎来给解答一下。

 #include<stdio.h>
#define maxn 2010
int father[maxn], gender[maxn];
int Find(int x)
{
if (father[x] != x)
{
int t = father[x];
father[x] = Find(father[x]);
gender[x] = (gender[x] + gender[t]) % ;
}
return father[x];
}
void Merge(int x,int y)
{
int fx = Find(x);
int fy = Find(y);
father[fx] = fy;
if (gender[y] == )
gender[fx] = ^ gender[x];
else gender[fx] = gender[x];
}
int main()
{
int t;
int cas = ;
//freopen("data.in", "r", stdin);
scanf("%d",&t);
while (t--)
{
int n, m;
scanf("%d%d",&n,&m);
for (int i = ; i <= n; i++)
{
father[i] = i;
gender[i] = ;
}
int ok = ;
while (m--)
{
int a, b;
scanf("%d%d",&a,&b);
if (!ok) continue;
if (Find(a) != Find(b))
Merge(a, b);
else if(gender[a] == gender[b])
ok = ;
}
if (ok) printf("Scenario #%d:\nNo suspicious bugs found!\n\n", cas++);
else printf("Scenario #%d:\nSuspicious bugs found!\n\n", cas++);
}
return ;
}

POJ 2492 A Bug's Life 并查集的应用的更多相关文章

  1. nyoj 209 + poj 2492 A Bug's Life (并查集)

    A Bug's Life 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 Background  Professor Hopper is researching th ...

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

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

  3. A Bug's Life POJ - 2492 (种类或带权并查集)

    这个题目的写法有很多,用二分图染色也可以写,思路很好想,这里我们用关于并查集的两种写法来做. 题目大意:输入x,y表示x和y交配,然后判断是否有同性恋. 1 带权并查集: 我们可以用边的权值来表示一种 ...

  4. POJ 1703 Find them, Catch them(并查集高级应用)

    手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/suncongbo/article/details/76735893 URL: http ...

  5. 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条臭 ...

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

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

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

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

  8. hdu 1829 &amp;poj 2492 A Bug&#39;s Life(推断二分图、带权并查集)

    A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  9. poj 2492 A Bug's Life 二分图染色 || 种类并查集

    题目链接 题意 有一种\(bug\),所有的交往只在异性间发生.现给出所有的交往列表,问是否有可疑的\(bug\)(进行同性交往). 思路 法一:种类并查集 参考:https://www.2cto.c ...

随机推荐

  1. Appium环境搭建及“fn must be a function”问题解决

    由于appium在线安装比较困难,大多数应该是由于FQ造成的吧,索性直接下载appium安装包:http://pan.baidu.com/s/1bpfrvjD nodejs下载也很缓慢,现提供node ...

  2. git pull免密码拉取

    ssh到服务器上,原来基于public/private key pair的方法不好使了. 1.1 创建文件存储GIT用户名和密码 在%HOME%目录中,一般为C:\users\Administrato ...

  3. itchat 动态注册

    动态注册时可以选择将itchat.run()放入另一线程或使用configured_reply()方法处理消息. 两种方法分别是: # 使用另一线程,但注意不要让程序运行终止 import threa ...

  4. 关于html头部引用(meta,link)

    /*这一段头部表示 如果安装了GCF,则使用GCF来渲染页面,如果为安装GCF,则使用最高版本的IE内核进行渲染.*/<meta content="IE=edge,chrome=1&q ...

  5. 【Trapping Rain Water】cpp

    题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...

  6. Python常见数据类型及操作

    基础数据类型 什么是数据类型? 我们人类可以很容易的分清数字与字符的区别,但计算机并不能,计算机虽然很强大,但从某种角度上看又很傻,除非你明确的告诉它,1是数字,“汉”是文字,否则它是分不清1和‘汉’ ...

  7. 再次理解javascript的apply

    普通函数执行的时候,this指向函数执行的上下文  其实就是一个原型链的结构...    我一直没有搞懂原型链莫非它们像链条一样连在一起?    昂...   原型链可以理解成继承吗?   就像,ja ...

  8. 优化脚本性能 Optimizing Script Performance

    This page gives some general hints for improving script performance on iOS. 此页面提供了一些一般的技巧,提高了在iOS上的脚 ...

  9. Educational Codeforces Round 22 E. Army Creation(分块好题)

    E. Army Creation time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  10. flink原理介绍-数据流编程模型v1.4

    数据流编程模型 抽象级别 程序和数据流 并行数据流 窗口 时间 有状态操作 检查点(checkpoint)容错 批量流处理 下一步 抽象级别 flink针对 流式/批处理 应用提供了不同的抽象级别. ...