您的位置:首页 > 其它

Corporative Network UVA - 1329

2017-10-25 20:29 453 查看
题目:https://vjudge.net/problem/UVA-1329

题目大意

给你n个点进行操作,I表示连接操作,A连接B就表示A指向B的中心。E 表示查询从某个点到中心的长度,点之间的长度等于它们做差的绝对值。

分析

In such a way the two old clusters are joined in a new cluster, served by the center of the old cluster B这句可以看出当一个点与另一个点连接,这个点的中心被另一个点的中心取代,这是并查集的性质。有位有距离属性,所以并查集加权。

代码

/********************************************************************
* File Name: Corporative_Network.cpp
* Author: Sequin
* mail: Catherine199787@outlook.com
* Created Time: 三  9/ 6 16:24:07 2017
*************************************************************************/

#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 30000
int p[maxn];
int dis[maxn];

int find(int x) {
if(x == p[x])    return x;
else{
int root = find(p[x]);
dis[x] += dis[p[x]];
p[x] = root;
return root;
}
}

int main() {
int n;
cin >> n;
while(n--) {

int N;
cin >> N;
for(int i = 1; i <= N; i++) {
dis[i] = 0;
p[i] = i;
}

char t;
while(cin >> t && t != 'O'){
if(t == 'E') {
int x;
cin >> x;
find(x);
cout << dis[x] << endl;
}

if(t == 'I') {
int a, b;
cin >> a >> b;
p[a] = b;
dis[a] = abs(b - a) % 1000;
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  并查集 权值 UVA ACM