UVa 1329 Corporative Network(并查集)
来源:互联网 发布:阿里云 招聘 编辑:程序博客网 时间:2024/05/04 17:45
题目链接:http://acm.hust.edu.cn/vjudge/problem/36132
大意:给出n个结点,一系列操作。对于操作I,输入u,v,把u的父结点设为v,距离为|u-v|mod1000;对于操作E,输入u,输出u到根结点的距离。
思路:两个操作正好对应了并查集的合并和查找,对于操作I,由于v没有父结点,直接令pre[u] = v即可;对于操作E,由于根节点是动态变化的,所以要先更新再查询。
#include<cstdio>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>using namespace std;const int maxn = 2e4 + 5;int n, pre[maxn], dist[maxn];int find(int x){ if (pre[x] == x) return pre[x]; int y = pre[x]; pre[x] = find(pre[x]); dist[x] += dist[y]; return pre[x];}int main(){ int T; scanf("%d",&T); while(T--) { scanf("%d",&n); memset(dist, 0, sizeof dist); for(int i = 1; i <= n; i++) pre[i] = i; char s[2]; while(scanf("%s",s) && s[0] != 'O') { if (s[0] == 'E') { int x; scanf("%d",&x); find(x); printf("%d\n",dist[x]); } else { int a,b; scanf("%d%d",&a,&b); pre[a] = b; dist[a] = abs(a-b) % 1000; } } }}
0 0
- UVA 1329Corporative Network【并查集】
- UVA 1329 Corporative Network(并查集)
- UVa:1329 Corporative Network(带权并查集)
- UVA - 1329 Corporative Network 合作网络(并查集)
- UVa 1329 Corporative Network(并查集)
- 1329 - Corporative Network(并查集)
- UVa 1329 Corporative Network (加权并查集)
- UVA 1329 Corporative Network(并查集:路径压缩)
- UVa 1329 Corporative Network(带权并查集)
- uva 1329 - Corporative Network(数据结构:并查集)
- UVA 1329 - Corporative Network(带权并查集)
- uva 1329 - Corporative Network(加权并查集)
- LA3027:Corporative Network(并查集) uva 1329
- UVA 1329 Corporative Network——加权并查集
- Corporative Network(带权并查集)
- UVALive - 3027Corporative Network(带权并查集)
- poj 1962 Corporative Network(并查集)
- UVALive - 3027 - Corporative Network (并查集!!)
- Android 开发中 app版本更新功能
- phpmyadmin安装配置
- 【bzoj1082】[SCOI2005]栅栏
- 【MyBatis】mybatis执行流程与缓存机制分析
- 2017网易内推笔试题---幸运的袋子
- UVa 1329 Corporative Network(并查集)
- SpringMVC基础知识(1)
- scala 学习笔记
- Foundation--NSSet
- sed指定某行插入、追加、全局替换
- Wpf DataTemplate 部分功能使用心得
- php编译安装详解(一)
- 5-21 求前缀表达式的值 (25分)
- spark 集群