poj--1703 Find them, Catch them(并查集)
来源:互联网 发布:python笔试选择题 编辑:程序博客网 时间:2024/04/29 11:17
problem link
题解
对于 D a b
,a和b属于不同的帮派,如果a
属于帮派A,那么a + n
属于帮派B,同理,b也是如此。
因此,并查集中元素的个数是2n
,每次合并就是Union(a, b + n) 和 Union(a + n, b)
这实际上是保留了所有的可能性,因为只知道a
和b
不在同一个集合里,不能确定到底是属于哪个集合。
#include <iostream>#include <algorithm>#include <vector>#include <cstdio>using namespace std;const int maxn = 100000 + 5;int p[2 * maxn];int n, m;int find(int x) { return x == p[x] ? x : p[x] = find(p[x]); }void Union(int x, int y){ x = find(x), y = find(y); if(x != y) p[x] = y;}int main(){#ifndef ONLINE_JUDGEfreopen("data.in", "r", stdin);#endif // ONLINE_JUDGE // 1500K 672MS int t; for(cin >> t; t--; ){ cin >> n >> m; for(int i = 0; i <= 2 * n; ++i) p[i] = i; char op; int a, b; for(int i = 0; i < m; ++i){ getchar(); scanf("%c %d %d", &op, &a, &b); if(op == 'A'){ if(find(a) == find(b)){ printf("In the same gang.\n"); } else if(find(a) == find(b + n)) printf("In different gangs.\n"); else printf("Not sure yet.\n"); } else{ Union(a, b + n); Union(a + n, b); } } } return 0;}
0 0
- poj 1703 Find them, Catch them(并查集)
- poj 1703 Find them, Catch them (并查集)
- poj 1703 -- Find them, Catch them(并查集)
- poj 1703 Find them, Catch them(并查集)
- poj 1703 Find them, Catch them (并查集)
- POJ 1703 Find them, Catch them (并查集)
- Find them, Catch them.(POJ-1703)(并查集)
- POJ - 1703 - Find them, Catch them (并查集)
- POJ 1703 Find them, Catch them(并查集)
- poj--1703 Find them, Catch them(并查集)
- POJ 1703 Find them, Catch them (并查集)
- poj 1703 Find them, Catch them(并查集)
- POJ 1703-Find them, Catch them(并查集)
- poj 1703 Find them, Catch them(并查集)
- Poj 1703 Find them, Catch them (并查集)
- POJ 1703 Find them, Catch them(并查集)
- POJ 1703 Find them, Catch them //并查集
- poj 1703 Find them, Catch them 并查集
- JVM分析工具
- 为什么任何对象都可以作为锁对象?
- typedef的使用
- 内部类、匿名内部类、内部接口比较
- 162. Find Peak Element
- poj--1703 Find them, Catch them(并查集)
- c语言练习
- docker pull命令实现与镜像存储(1)
- crypt1<uscao>1.3
- 0003算法笔记——【分治法】分治法与二分搜索,棋盘覆盖问题
- IOS学习(六)导航视图控制器
- 递归函数
- Function pointer - 函数指针
- Docker学习笔记 --- Iaas,Paas,SaaS详解