POJ1703--Find them, Catch them
来源:互联网 发布:软件测试linux常用命令 编辑:程序博客网 时间:2024/06/05 09:40
题目大意:有N个罪犯,分属于两个团伙。有两种操作,一、D表示两个罪犯不在同一个团伙,二、A表示判断两个罪犯是否属于同一个团伙。操作m次
分析:对每个罪犯创建2个元素,i-x,i-y,都用并查集维护。i-x表示i属于x类。查询的时候判断i-x与j-x,i-x与j-y的关系即可。合并的时候,合并i-x与j-y,j-x与i-y。
代码:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 111111;int n, m;int fa[2*maxn+1];int Find(int x) { return fa[x] == x ? x : fa[x] = Find(fa[x]);}void Union(int x, int y) { int p1 = Find(x); int p2 = Find(y); if(p1 != p2) fa[p1] = p2;}int main() { int T; scanf("%d", &T); while(T--) { scanf("%d%d", &n, &m); for(int i = 1; i <= n; i++) { fa[i] = i; fa[n+i] = n+i; } char op[2]; while(m--) { int a, b; scanf("%s%d%d", op, &a, &b); int p1 = Find(a); int p2 = Find(b); int p3 = Find(n+a); int p4 = Find(n+b); if(op[0] == 'A') { if(p1 != p2 && p1 != p4) printf("Not sure yet.\n"); else if(p1 != p2 || p3 != p4) printf("In different gangs.\n"); else printf("In the same gang.\n"); } else { Union(a, b+n); Union(a+n, b); } } } return 0;}
0 0
- poj1703 - Find them, Catch them
- POJ1703 Find them, Catch them
- poj1703 Find them, Catch them
- poj1703 Find them, Catch them
- POJ1703 Find them,Catch them
- POJ1703--Find them, Catch them
- poj1703 find them,catch them
- Find them, Catch them poj1703
- POJ1703 Find them, Catch them
- poj1703 Find them, Catch them
- poj1703 Find them, Catch them
- POJ1703-Find them, Catch them
- POJ1703 Find them, Catch them
- poj1703 Find them, Catch them
- poj1703 Find them, Catch them
- poj1703——Find them, Catch them
- poj1703——Find them, Catch them
- Find them, Catch them (poj1703)
- Unrecognized Windows Sockets error: 0
- uva 575
- Meclipse 10 设置 new 菜单
- POJ3614--Sunscreen
- tomcat8配置(win8)
- POJ1703--Find them, Catch them
- 第五周工作周报
- 分解质因数 模板 ACM
- maven的jetty插件提示No Transaction manager found导致启动慢的解决方法
- FZU-2148 Moon Game
- linux:vim基础
- POJ1182--食物链
- Java实现数据结构与算法-链表与双向链表
- HDU 5343(MZL's Circle Zhou-SAM)[Template:SAM]