Corporative Network UVA - 1329

题目: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;
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值