poj1703 Find them, Catch them(关系并查集)
来源:互联网 发布:淘宝充值网易宝可信嘛 编辑:程序博客网 时间:2024/05/19 22:57
http://poj.org/problem?id=1703
题意:给你n个人,m个操作,A x y代表查询x和y是否是同类,有是、否、不确定三种输出;D x y代表确定x和y不同类。
思路:食物链的简化版,那个是三种关系,而这个只有两种。推公式就按照食物链的思路推,纸上比划下就出来了。一旦判断了两个元素的关系,就把他们合并为一个集合。
#include <stdio.h>#include <algorithm>#include <stdlib.h>#include <string.h>#include <iostream>using namespace std;typedef long long LL;const int N = 100010;const int INF = 0x3f3f3f3f;int pre[N], r[N];int Find(int x){ if(x == pre[x]) return x; int tmp = pre[x]; pre[x] = Find(pre[x]); r[x] = (r[x]+r[tmp])%2; return pre[x];}void Union(int x, int y){ int rx = Find(x); int ry = Find(y); pre[rx] = ry; r[rx] = (2-r[x]+1+r[y])%2;}int main(){ // freopen("in.txt", "r", stdin); int t, n, m, x, y; char s[5]; scanf("%d", &t); while(t--) { scanf("%d%d", &n, &m); for(int i = 1; i <= n; i++) { pre[i] = i; r[i] = 0; } while(m--) { scanf("%s", s); scanf("%d%d", &x, &y); if(s[0] == 'A') { int rx = Find(x); int ry = Find(y); if(rx != ry) { printf("Not sure yet.\n"); } else { if(r[x] != r[y]) printf("In different gangs.\n"); else printf("In the same gang.\n"); } } else if(s[0] == 'D') { Union(x, y); } } } return 0;}
1 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 并查集
- 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 种类并查集
- poj1703 Find them, Catch them 基础并查集问题
- poj Find them, Catch them(关系并查集)
- ACM--多边形面积--HDOJ 2036--改革春风吹满地
- vs打开低版本的工程报错
- 单周期CPU设计
- Android插件化探索(一)类加载器DexClassLoader
- TensorFlow编译过程中遇到的问题及解决方案
- poj1703 Find them, Catch them(关系并查集)
- 【Leetcode】:9. Palindrome Number 问题 in JAVA
- C primer plus 第八章 练习6:
- python 2.7中安装mysql
- 深入理解JVM08--虚拟机执行子系统--方法调用详解(解析、分派)
- 配置文件解析利器-Config库
- 树转链表
- ARM的PC和LR寄存器
- hdu 3037 Saving Beans (大组合数取模--Lucas定理)