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


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. 捕获arm托管磁盘虚拟机,并进行还原

    背景:托管磁盘虚拟机"hlmma69n2",附加了一块100GB的数据磁盘.可以通过Portal管理界面直接捕获该虚拟机并进行还原,详情见如下步骤: 1.在虚拟机内部执行一般化的操 ...

  2. SQL 优化经验总结34条(转)

    (1) 选择最有效率的表名顺序(只在基于规则的优化器中有效): ORACLE 的解析器按照从右到左的顺序处理FROM子句中的表名,FROM子句中写在最后的表(基础表 driving table)将被最 ...

  3. Visionpro学习笔记 :QuickBuild-Based Application Run-Once Button

    1) Creating a Run-Once Button 通过JobManager调用VisionPro文件.所有的过程放到一个Try/Catch块中. Private Sub RunOnceBut ...

  4. 【转】shell学习笔记(一)——学习目的性、特殊字符、运算符等

    1 学习shell的目的性 写之前我们先来搞清楚为什么要学shell,学习要有目的性 shell简单.灵活.高效,特别适合处理一些系统管理方面的小问题 shell可以实现自动化管理,让系统管理员的工作 ...

  5. Java多线程优化方法及使用方式

    一.多线程介绍 在编程中,我们不可逃避的会遇到多线程的编程问题,因为在大多数的业务系统中需要并发处理,如果是在并发的场景中,多线程就非常重要了.另外,我们在面试的时候,面试官通常也会问到我们关于多线程 ...

  6. 多对多中间表详解 -- Django从入门到精通系列教程

    该系列教程系个人原创,并完整发布在个人官网刘江的博客和教程 所有转载本文者,需在顶部显著位置注明原作者及www.liujiangblog.com官网地址. Python及Django学习QQ群:453 ...

  7. Win10微软帐户切换不回Administrator本地帐户的解决方法--(转,虽转但亲测有效)

    在Win10系统中经常会用到微软帐户登录,如应用商店等地方,不过一些用户反馈原来使用Administrator帐户被绑定微软帐户后无法切换回本地帐户,连[改用本地帐户登录]按钮都没有,那么怎么解决呢? ...

  8. cdh版本的hive安装以及配置

    hive依赖hadoop 需要的软件包:hive-0.13.1-cdh5.3.6.tar.gz .hadoop-2.5.0-cdh5.3.6.tar.gz 1.hadoop的安装步骤请访问: http ...

  9. BZOJ 1226: [SDOI2009]学校食堂Dining [DP 状压]

    题意: $n$个人排队打饭,第$i$个人口味$a_i$,能容忍最多身后第$b_i$个人先打饭. 先后两人$i,j$做饭时间为$a_i & a_j - a_i | a_j$ 求最少时间 一开始想 ...

  10. BZOJ 2194 [快速傅里叶变换 卷积]

    题意:请计算C[k]=sigma(a[i]*b[i-k]) 其中 k < = i < n ,并且有 n < = 10 ^ 5. a,b中的元素均为小于等于100的非负整数. 卷积 ( ...