题意:有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. 序列化模块--json模块--pickle模块-shelve模块

    什么叫序列化? 序列化是指把内存里的数据类型转变成字符串,以使其能存储到硬盘或通过网络传播到远程,因为硬盘或网络传输时只能接受bytes 例: 把内存数据 转成字符 # data ={# 'roles ...

  2. 【NopCommerce 3.1】asp.net mvc 利用jQuery from.js上传用户头像

    纯代码不解释. 在CusotmerControllers中添加上传方法 /// <summary> /// ajax上传用户头像 /// </summary> /// < ...

  3. WPF TextBlock 调整下划线与文字的距离

    <TextBlock Foreground="> <TextBlock.TextDecorations> <TextDecorationCollection&g ...

  4. 聊聊、CA机构认证CSR生成

    https://search.thawte.com/support/ssl-digital-certificates/index?page=content&id=SO832 https://s ...

  5. BZOJ 3437:小P的牧场(DP+斜率优化)

    小P的牧场[题目描述]背景:小P 是个特么喜欢玩MC 的孩纸...小P 在MC 里有n 个牧场,自西向东呈一字形排列(自西向东用1…n 编号),于是他就烦恼了:为了控制这n 个牧场,他需要在某些牧场上 ...

  6. TOJ 4105 Lines Counting(离线树状数组)

    4105.   Lines Counting Time Limit: 2.0 Seconds   Memory Limit: 150000K Total Runs: 152   Accepted Ru ...

  7. swiper单屏滚动

    .swiper-slide { overflow: auto; } 1. 排除某些屏,不滚动 var startScroll, touchStart, touchCurrent; var aSlide ...

  8. git 以及 工作区 版本库 暂存区

    https://www.jianshu.com/p/a308acded2ce            这个博客介绍的比较简单 https://blog.csdn.net/qq_31828515/arti ...

  9. JavaScript 的新特性:类的 #private 字段

    这是什么,如何使用,为什么需要? 一边听“Noise Pollution” —— Portugal. The Man,一边阅读本文简直就是享受 JavaScript 标准的第二阶段(Stage 2)加 ...

  10. 【CF1073A】Diverse Substring(签到)

    题意:给定一个由小写字母组成的串,要求找出一个子串使得其中出现最多的字母不超过它长度的一半下取整 n<=1e3 思路: #include<cstdio> #include<cs ...