UVa:1329 Corporative Network(带权并查集)
来源:互联网 发布:安卓模拟器对比 知乎 编辑:程序博客网 时间:2024/05/23 01:56
题意:有两种操作。1,将u设为v的父亲结点,距离为abs(u-v)%1000;2,求u到根节点的距离。
思路:带权并查集。设dist[u]表示u到根节点的距离,每次压缩路径时,需要每次加上其父亲结点的dist。另外在合并时计算为dist[v]=abs(u-v)%1000+dist[u]。
#include <iostream>#include <algorithm>#include <cmath>using namespace std;const int mod=1000;const int maxn=20005;int father[maxn];int dist[maxn];void init(int n){ for(int i=1; i<=n; ++i) father[i]=i,dist[i]=0;}int find(int p){ if(p==father[p]) return p; else { int q=father[p]; father[p]=find(father[p]); dist[p]+=dist[q]; return father[p]; }}//将a的父亲设为b的祖先void unio(int a,int b){ father[a]=b; dist[a]=abs(a-b)%mod;}int main(){ int T; scanf("%d",&T); while(T--) { int n; scanf("%d",&n); init(n); char p[3]; int a,b; while(scanf("%s",p)&&p[0]!='O') { if(p[0]=='E') { scanf("%d",&a); find(a); printf("%d\n",dist[a]); } else { scanf("%d%d",&a,&b); unio(a,b); } } } return 0;}
0 0
- UVa:1329 Corporative Network(带权并查集)
- UVa 1329 Corporative Network(带权并查集)
- UVA 1329 - Corporative Network(带权并查集)
- UVA 1329Corporative Network【并查集】
- UVA 1329 Corporative Network(并查集)
- Corporative Network(带权并查集)
- UVALive - 3027Corporative Network(带权并查集)
- poj 1962 Corporative Network(带权并查集)
- POJ - 1962 Corporative Network(带权并查集)
- UVALive 3027 Corporative Network (带权并查集)
- poj1962 Corporative Network(带权并查集)
- UVA - 1329 Corporative Network 合作网络(并查集)
- UVa 1329 Corporative Network(并查集)
- 1329 - Corporative Network(并查集)
- LA3027 Corporative Network 带权并查集入门
- UVa 1329 Corporative Network (加权并查集)
- UVA 1329 Corporative Network(并查集:路径压缩)
- uva 1329 - Corporative Network(数据结构:并查集)
- Medoo Has的使用:判断根据查询条件得出的值是否存在
- ExtJs日期控件案例(可控制时间的选择)
- POJ1306 Combinations
- LeetCode题目:Unique Binary Search Trees,一维动态规划
- C# 中的委托和事件
- UVa:1329 Corporative Network(带权并查集)
- Java第七课
- URL提交 中文乱码在Tomcat也要处理
- Java自学之路前沿
- Ogre材质解析代码初步分析(二)
- Linux进程状态(ps stat)之R、S、D、T、Z、X
- Intellij IDEA 提交代码到远程GitHub仓库
- 三个线程交替执行,c++实现
- 聚集索引和非聚集索引(整理)