这题比较简单,注意路径压缩即可。


AC代码

//#define LOCAL
#include <stdio.h>
#include <algorithm>
using namespace std;
const int maxn = 20000+5;
int par[maxn], dis[maxn];

void init(int n) {
    for(int i = 0; i <= n; i++) {
        par[i] = i;
        dis[i] = 0;
    }
}

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

int main() {
#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif // LOCAL

    int T, n;
    scanf("%d", &T);
    while(T--) {
        scanf("%d", &n);
        init(n);
        char cmd[10];
        int x, y;
        while(scanf("%s", cmd) == 1 && cmd[0] != 'O') {
            char tag = cmd[0];
            if(tag == 'E') {
                scanf("%d", &x);
                findRoot(x);
                printf("%d\n", dis[x]);
            } else {
                scanf("%d%d", &x, &y);
                par[x] = y;
                dis[x] = abs(x-y) % 1000;
            }
        }

    }
    return 0;
}

如有不当之处欢迎指出!

UVALive - 3027 Corporative Network (并查集)的更多相关文章

  1. [LA] 3027 - Corporative Network [并查集]

    A very big corporation is developing its corporative network. In the beginning each of the N enterpr ...

  2. LA 3027 Corporative Network 并查集记录点到根的距离

    Corporative Network Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [S ...

  3. 【暑假】[实用数据结构]UVAlive 3027 Corporative Network

    UVAlive 3027 Corporative Network 题目:   Corporative Network Time Limit: 3000MS   Memory Limit: 30000K ...

  4. UVALive 3027 Corporative Network 带权并查集

                         Corporative Network A very big corporation is developing its corporative networ ...

  5. 并查集 + 路径压缩(经典) UVALive 3027 Corporative Network

    Corporative Network Problem's Link Mean: 有n个结点,一开始所有结点都是相互独立的,有两种操作: I u v:把v设为u的父节点,edge(u,v)的距离为ab ...

  6. UVALive 3027 Corporative Network (带权并查集)

    题意: 有 n 个节点,初始时每个节点的父节点都不存在,你的任务是执行一次 I 操作 和 E 操作,含义如下: I  u  v   :  把节点 u  的父节点设为 v  ,距离为| u - v | ...

  7. UVALive 3027 Corporative Network

    ---恢复内容开始--- Corporative Network Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld ...

  8. 3027 - Corporative Network

    3027 - Corporative Network 思路:并查集: cost记录当前点到根节点的距离,每次合并时路径压缩将cost更新. 1 #include<stdio.h> 2 #i ...

  9. 3027 - Corporative Network(并差集)

    3027 - Corporative Network A very big corporation is developing its corporative network. In the begi ...

随机推荐

  1. python 中 reversed()函数

    一个列表a: a=[1,2,3,4,5,6,7] 一个对象b: b=reversed(a) 输出: print(b) <list_reverseiterator object at 0x0000 ...

  2. maven系列--maven目录

    我们在玩maven,首先就是利用maven来管理我们的项目.其实maven并不难,它无非是一种目录结构.所以在本系列开始之前,我们要细致的了解下maven的目录,其实也就是maven的约定. 约定优于 ...

  3. 【Java框架型项目从入门到装逼】第十四节 查询用户列表展现到页面

    这一节,我们来实现一下用户列表搜索,最终的效果如下: 这边我们要使用easyUI给我们提供的datagrid组件. HTML结构如下 <!-- 数据列表 --> <table id= ...

  4. CentOS之7与6的区别

    前言 centos7与6之间最大的差别就是初始化技术的不同,7采用的初始化技术是Systemd,并行的运行方式,除了这一点之外,服务启动.开机启动文件.网络命令方面等等,都说6有所不同.让我们先来了解 ...

  5. javacript 组合使用构造函数模式和原型模式

    构造函数模式创建对象 基本方法 function Person(name,age){ this.name=name; this.age=age; this.sayName=function(){ al ...

  6. web移动端常见问题解决方案 (转)

    总结:本文总结了web移动端的常见问题并附上解决方案,包括:Meta标签.获取滚动条的值.禁止选择文本.屏蔽阴影.css之border-box.css3多文本换行.Retina屏幕高清图片.html5 ...

  7. Cisco配置aaa验证

    当您的网络中部署了一台集中的radius校验服务器(比如我司的SAM,cisco的ACS等),希望对登陆设备的用户身份进行合法性校验,而账号都统一由该radius服务器集中产生与维护,您希望所有的登入 ...

  8. rabbitmq的构架和原理(三)

    前面两篇博文已经将环境安装和相关配置介绍了,现在开始正式学习rabbitmq的使用了: rabbitMQ的构架 rabbitmq作为消息队列,一条消息从发布到订阅消费的完整流程为: 消息 --> ...

  9. 对List中每个对象元素按时间顺序排序

    需求: 需要对List中的每个User按照birthday顺序排序,时间由小到大排列. 代码实现: import java.text.SimpleDateFormat; import java.uti ...

  10. CentOS(Linux)下安装dmidecode包

    安装代码: yum install dmidecode 安装完成后,查看总体信息: dmidecode 查看服务器类型,测试环境为DELL R610: dmidecode -s system-prod ...