UVALive - 3027Corporative Network(带权并查集)
来源:互联网 发布:京东云 阿里云 编辑:程序博客网 时间:2024/05/02 00:31
题目: UVALive - 3027Corporative Network(带权并查集)
题目大意:有n和节点,初始时每个节点的父节点都不存在,然后有下面两种操作:I 操作 I a,b 将a的父节点变成b。E操作 E a,查询a到它的父节点的距离。
解题思路:带权并查集。注意这里距离的变化是a -> b,那么a到根节点的距离就是a到b的距离的绝对值 % 1000 + b到它的根节点的距离。
代码:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 2e5 + 5;int p[maxn], c[maxn];int n;void init () {for (int i = 1; i <= n; i++) {p[i] = i;c[i] = 0;}}int getParent (int a) {if (a == p[a])return a;int t = p[a];p[a] = getParent (p[a]);c[a] += c[t];return p[a];}int main () {int T;int a, b;char str[10];scanf ("%d", &T);while (T--) {scanf ("%d", &n);init();while (scanf ("%s", str) != EOF) {if (str[0] == 'O')break;if (str[0] == 'E') {scanf ("%d", &a);int q1 = getParent (a);printf ("%d\n", c[a]);} else {scanf ("%d%d", &a, &b);p[a] = b;c[a] = abs (a - b) % 1000;}}}return 0;}
0 0
- UVALive - 3027Corporative Network(带权并查集)
- UVALive 3027 Corporative Network (带权并查集)
- UVALive - 3027 Corporative Network 并查集
- UVALive - 3027 - Corporative Network (并查集!!)
- uvalive 3027 Corporative Network(并查集)
- uvalive 3027 Corporative Network(种类并查集)
- UVa:1329 Corporative Network(带权并查集)
- Corporative Network(带权并查集)
- poj 1962 Corporative Network(带权并查集)
- POJ - 1962 Corporative Network(带权并查集)
- poj1962 Corporative Network(带权并查集)
- Corporative Network +uvalive+并查集
- uvalive 3027 Corporative Network 并查集+路径压缩
- UVALive - 3027 Corporative Network 并查集+向量
- UVALive 3027---Corporative Network+并查集的应用
- 指南 第三章 例题6 UVALive 3027 Corporative Network(并查集的应用)
- UVa 1329 Corporative Network(带权并查集)
- UVA 1329 - Corporative Network(带权并查集)
- jsp中 中文处理
- SQL中CONVERT转化函数的用法
- 码农提高工作效率
- JavaSe基础XX18——IO流_4
- 利用博客提交作业的程序设计课程实践模式
- UVALive - 3027Corporative Network(带权并查集)
- IOS - 自定义表格(UITableView)
- oracle 对象统计信息
- 好的用户界面-界面设计的一些技巧
- 媒体播放
- oralce 新建用户
- JVM 调优总结
- 布隆过滤器(Bloom Filter)Java实现
- C语言 数据类型2