POJ - 1703 Find them, Catch them(带权并查集)
来源:互联网 发布:天刀捏脸数据成男网盘 编辑:程序博客网 时间:2024/06/05 07:42
题目大意:有两个帮派,N个人,现在有M个操作
A u v:表示询问,问u和v是不是属于同一个帮派的
D u v:表示u和v是在不同的帮派的
解题思路:带权并查集,刚开始都初始化为0,剩下的操作就是带权并查集模版了
如果u和v到根的权值不同的话,表示两个人帮派不同,如果相同的话,就表示帮派相同了
#include <cstdio>#include <cstring>const int N = 100010;int n, m;int f[N], r[N];void init() { for (int i = 1; i <= n; i++) { f[i] = i; r[i] = 0; }}int find(int x) { if (x == f[x]) return x; int t = find(f[x]); r[x] = (r[x] + r[f[x]]) % 2; return f[x] = t;}void solve() { char str[10]; int x, y; while (m--) { scanf("%s%d%d", str, &x, &y); int tx = find(x); int ty = find(y); if (str[0] == 'A') { if (tx != ty) { printf("Not sure yet.\n"); } else if (r[x] != r[y]) { printf("In different gangs.\n"); } else printf("In the same gang.\n"); } else { f[tx] = ty; r[tx] = (-r[x] + 1 + r[y] + 2) % 2; } }}int main() { int test; scanf("%d", &test); while (test--) { scanf("%d%d", &n, &m); init(); solve(); } 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 带权并查集OR种类并查集
- 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 1035 解题报告
- 面试中的Singleton【转】
- Servlet获取请求参数、表单验证(2015.9.15)
- 初识OS
- C语言之位操作符
- POJ - 1703 Find them, Catch them(带权并查集)
- sparksql性能调优
- Python中的Berkeley DB(1):Hello Berkeley DB
- 【LeetCode】Integer to English Words 解题报告
- 自动分表
- 浅谈jsp的Session的生命周期
- 欢迎使用CSDN-markdown编辑器
- Aizu 2541 Magical Bridges
- win7下远程桌面访问ubuntu15.04