poj 1703 poj 2492 并查集 桥梁判AB组
poj 1703:
题意:
有N名来自两个帮派的小弟,已知一些小弟不属于同一帮派。
判断给定两个小弟是否属于同一帮派。
解析:
并查集。
A表示小弟在帮派A,B表示小弟在帮派B。
将所有点拆成两组,当做桥梁。
样例:
5 5 A 1 2 D 1 2 A 1 2 D 2 4 A 1 4
如下图所示:
1. A 1 2
初始访问,肯定是不确定的。
2.D 1 2
得知两者不在一个帮派,所以将 1 和 2 + n = 7 并入并查集,这样7就当做了一条桥梁,如果还有与2这个点不同帮派的,就直接将其归入了1门下。
同理,将 2 和 1 + n = 6 并入并查集。
3.A 1 2
此时,1 和 2 + n = 7 属于同一连通块,所以他们不在同一帮派。
4.D 2 4
这一步是理解桥梁作用的重点。
得知,2 与 4 不再同一个帮派,所以 2 与 4 + n = 9 合并,4 与 2 + n = 7 合并。
此时 7 便作为了逻辑上的桥梁,将 1 与 4 同一帮派的连在了一起。
5.A 1 4
因而,这俩货在同一个帮派。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long long
#define lson lo, mi, rt << 1
#define rson mi + 1, hi, rt << 1 | 1using namespace std;
const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1.0);
const double ee = exp(1.0);int fa[maxn << 1];
int height[maxn << 1];void Init(int n)
{for (int i = 1; i <= n; i++){fa[i] = i;height[i] = 0;}
}int Find(int x)
{if (fa[x] == x)return x;elsereturn fa[x] = Find(fa[x]);
}void Union(int u, int v)
{int fau = Find(u);int fav = Find(v);if (fau == fav)return;if (height[fau] < height[fav]){fa[fau] = fav;}else{fa[fav] = fau;if (height[fau] == height[fav]){height[fau]++;}}
}bool Same(int u, int v)
{return Find(u) == Find(v);
}int main()
{
#ifdef LOCALfreopen("in.txt", "r", stdin);
#endif // LOCALint ncase;scanf("%d", &ncase);while (ncase--){int n, m;scanf("%d%d", &n, &m);Init(n << 1);while (m--){char op[5];int fr, to;scanf("%s", op);scanf("%d%d", &fr, &to);if (op[0] == 'A'){if (Same(fr, to)){printf("In the same gang.\n");}else if (Same(fr, to + n)){printf("In different gangs.\n");}else{printf("Not sure yet.\n");}}else{Union(fr, to + n);Union(fr + n, to);}}}return 0;
}
poj 2492:
题意:
有一种昆虫n只,他们的标号1-n,现在做了交配试验(雾),来判断他们是否有同性恋存在。
每次给出两只昆虫的标号,代表他们在交配(大雾)。
解析:
同上题,帮派变成了性别。
每次给出的两只昆虫应该是不同性别的,因此加入并查集。
若属同一性别,则试验失败。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long long
#define lson lo, mi, rt << 1
#define rson mi + 1, hi, rt << 1 | 1using namespace std;
const int maxn = 2000 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1.0);
const double ee = exp(1.0);int fa[maxn << 1];
int height[maxn << 1];void Init(int n)
{for (int i = 1; i <= n; i++){fa[i] = i;height[i] = 0;}
}int Find(int x)
{if (fa[x] == x)return x;elsereturn fa[x] = Find(fa[x]);
}void Union(int u, int v)
{int fau = Find(u);int fav = Find(v);if (fau == fav)return;if (height[fau] < height[fav]){fa[fau] = fav;}else{fa[fav] = fau;if (height[fau] == height[fav]){height[fau]++;}}
}bool Same(int u, int v)
{return Find(u) == Find(v);
}int main()
{
#ifdef LOCALfreopen("in.txt", "r", stdin);
#endif // LOCALint ncase;scanf("%d", &ncase);int ca = 1;while (ncase--){int n, m;scanf("%d%d", &n, &m);Init(n << 1);bool flag = true;while (m--){int fr, to;scanf("%d%d", &fr, &to);if (Same(fr, to)){flag = false;}Union(fr, to + n);Union(fr + n, to);}printf("Scenario #%d:\n", ca++);if (!flag)printf("Suspicious bugs found!\n\n");elseprintf("No suspicious bugs found!\n\n");}return 0;
}
提高:
食物链:
poj 1182 食物链
poj 1703 poj 2492 并查集 桥梁判AB组相关推荐
- Find them, Catch them POJ - 1703(种类并查集)
题意: 在这个城市里有两个黑帮团伙,现在给出N个人,问任意两个人他们是否在同一个团伙 1.输入D x y代表x于y不在一个团伙里 2.输入A x y要输出x与y是否在同一团伙或者不确定他们在同一个团伙 ...
- POJ 2492 并查集,带权或带偏移量并查集 【例题详解】
分析这道题,我们可以得出,假如a,b,c三只虫子,a和b可以交配,b和c也可以交配,那么a和c一定是同行,因为他们和同一个异性交配了.所以我们可以把关系抽象成图,在ab之间,bc之间各连一条线长度为1 ...
- POJ 1182 食物链,并查集的拓展
http://poj.org/problem?id=1182 /******************************************************** 此道题目 前天看的时候 ...
- POJ 2236 Wireless Network 并查集
Wireless Network 并查集 Crawling in process... Crawling failed Time Limit:10000MS Memory Limit:6553 ...
- POJ 1417 True Liars 并查集+背包
题目链接:http://poj.org/problem?id=1417 解题思路:比较容易想到的是并查集,然后把第三组数据测试一下之后发现这并不是简单的并查集,而是需要合并之后然后判断的.并且鉴于题目 ...
- poj 1456 Supermarket 贪心+并查集(个人感觉有点难判断出来
poj 1456 这第一眼还觉得只要贪心就可以了,但是emmm看了大佬的题解居然真的要用到并查集= = 大佬清晰的思路 大佬舒服的代码 #pragma warning(disable:4996) #i ...
- poj 3723 Conscription (并查集)
1 首先我们应该区分开男孩和女孩,只要将男孩的编号加上女孩的个数n,这样就可以做到男孩和女孩的编号是不同的. 2 题目中说了如果两个人有关系,并且其中一个人已经被选了那么选择另外一个人的时候只要100 ...
- POJ 1182 食物链 (并查集解法)(详细注释)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 78510 Accepted: 23396 Description 动物王 ...
- POJ 2236 - Wireless Network ( 并查集 )
题意 一个计算机网络里的计算机都坏了, 现在有两种操作, "O p"代表修复了p机器, "S p q"代表检查 p, q 两台机器是否连接( 直线距离<= ...
- POJ 1182 食物链(并查集+偏移向量)题解
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 82346 Accepted: 24616 Description ...
最新文章
- .NET中可空值类型实现原理
- C++ 文件和文件流
- apache camel_Apache Camel中的短重试与长重试
- Django first lesson 环境搭建
- LeerCode 题 - 14. 最长公共前缀
- NanoDet:这是个小于4M超轻量目标检测模型
- 年度大战即将拉开!iPhone 11系列后,轮到华为Mate30系列表演了
- 在线XML转HTML工具
- OpenLayers之官网实例
- 「代码随想录」121. 买卖股票的最佳时机【贪心】【动态规划】力扣/leetcode详解
- Java基础002 Java9开发环境搭建(Win10平台)
- 多电平双向DC/DC直流变换器的工作原理(以三电平为例子)
- 以结果为导向的项目管理
- IDEA 查看项目编码
- 如何高效使用Micropython看你喜欢看的视频?【下】(生成图片帧和WIFI传输播放)
- Python 输出[m,n]之间既能被3整除又能被7整除的数
- FotoMagico Pro 5.6.6 特别版 Mac 优秀电子视频相册制作工具软件
- 【功能实现】qrcode生成二维码Demo
- 经济学day01 微观经济学和宏观经济学
- Sharding-JDBC + SpringBoot
热门文章
- 大数据hive篇--同比环比
- 一个移动开发老码农的书单
- Intel Thunderbolt 3 接口介绍
- 哪个数据库替代oracle,关于Oracle数据库替代加密算法
- MacBook Pro的touchbar疯狂闪烁 通过写程序不断点亮touchbar,从而避免其闪烁
- leetcode_83_Remove Duplicates from Sorted List
- 窗口函数preceding和following字段
- 用Python解决女朋友看电影没字幕的需求
- 【容斥原理】(AtCoder Regular Contest 093 F) Dark Horse
- Chromium网页CPU光栅化原理分析