uvalive3027 Corporative Network
来源:互联网 发布:南京浦口行知基地图片 编辑:程序博客网 时间:2024/06/18 03:22
题目链接:https://vjudge.net/problem/UVALive-3027
题意:
有个两个操作,I操作把u的父节点更新为v,把距离更新为abs(u-v)%1000,保证在这之前,u没有父节点。E操作询问u到根的距离。
思路:
I操作保证了在这之前u没有父节点,那么可以直接pa[u]=v; d[u] = abs(u-v)%1000;
E操作可能是访问I操作中u的儿子节点到父节点的距离或者是u的父节点v还有父节点,此时需要一次find()操作,把路径距离更新。
代码
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>using namespace std;const int maxn = 20000 + 10;int pa[maxn], d[maxn];void init(){ for(int i=0; i<maxn; ++i){ pa[i] = i; d[i] = 0; }}int find(int x){ if(pa[x] == x) return x; int rt = find(pa[x]); d[x] += d[pa[x]]; return (pa[x] = rt);}int main(){ int t, n, u, v; char opt; cin>>t; while(t--){ cin>>n; init(); while(true){ cin>>opt; if(opt=='I'){ cin>>u>>v; pa[u] = v; d[u] = abs(u-v) % 1000; } else if(opt=='E'){ cin>>u; find(u); cout<<d[u]<<endl; } else{ break; } } } return 0;}
阅读全文
0 0
- uvalive3027 Corporative Network
- 【并查集】UVALive3027 Corporative Network
- Corporative Network
- Corporative Network
- Corporative Network
- Corporative Network
- poj Corporative Network
- poj 1962 Corporative Network
- LA - 3027 - Corporative Network
- LA--3027--Corporative Network
- POJ1962 Corporative Network
- POJ-1962-Corporative Network
- poj1962 Corporative Network
- LA 3027 Corporative Network
- LA 3027 - Corporative Network
- UVALive 3027 Corporative Network
- UVALive 3027 Corporative Network
- LA 3027 Corporative Network
- const和static
- Oracle Instant Client(即时客户端) 安装与配置
- STM32F429 时钟系统笔记
- VS2010 类库程序集 无法添加WPF窗口、WPF用户控件
- PyQt5笔记
- uvalive3027 Corporative Network
- 趣谈设计模式
- OpenGL 可编程管线学习
- IntelliJ Idea 集成svn 和使用
- 关于oracle rowid 自动排序的理解
- idea git 小技巧
- WPF使用MVVM时,表单验证
- matlab画图的一些问题
- 【数论-莫比乌斯入门】hdu 1695 GCD