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


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. Windows任务计划程序起始于参数自动修改

    Windows任务计划程序建立后,手工运行可以成功,但计划任务自动运行却不能成功,搜索网络,原来是起始于参数没有配置,这个参数的英文名字是start-in.它保证任务计划程序的WorkingDirec ...

  2. C语言学习之交换(冒泡)排序

    在学习c语言的过程中,在数组内容中我们总是能学习到对一组数据进行排序,对于排序有许多的方法,像 (交换)冒泡排序.选择排序.(基数)桶排序.(插入)二分法排序等等. 我主要以我个人的理解去分析常见的交 ...

  3. [Java 教程 01] Hello,Java!

    前言 从事编程已经有一段时间了,突然发现,Java作为我的第一编程语言,自己似乎对她并有一个系统的思想.当下Java依旧保持着超高的热度,新特性也不断出现,从当初学习的java6版本到最近刚出的jav ...

  4. zabbix_sender用法实例

    环境centos6.8 zabbix版本3.2.4 需求: 要远程监控一台服务器A,但只能通过远程服务器连接本地服务器B,但B不能主动连A(因为A没有固定公网ip) 使用了zabbix_agent的a ...

  5. MySQL权限详解

    MySQL权限级别介绍 MySQL权限级别 全局性的管理权限,作用于整个MySQL实例级别 数据库级别的权限,作用于某个指定的数据库上或者所有的数据库上 数据库对象级别的权限,作用于指定的数据库对象上 ...

  6. 面向对象_03【关键字:final使用】

    final关键字:可修饰类.变量名和方法1,final修饰的类不能被继承2,final修饰的变量(成员.局部)是常量,只能赋值一次.3,final修饰的方法不能被子类重写Example:一:修饰类 / ...

  7. The man Command

    The man command is used to format and display the man pages. The man pages are a user manual that is ...

  8. 自定义Func模块

    自定义Func模块 (1)自定义模块步骤 (2)生成模块 [root@controller modules]# cd /usr/lib/python2.7/site-packages/func/min ...

  9. sed运用

    流编辑器sed sed简介 sed是stream editor的缩写,一种流编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为"模式空间"(pattern ...

  10. SQL Server判断是否满足日期格式(YYYYMMDD)以及中文等判断,格式化为YYYY-MM-DD

    SQL Server判断是否满足日期格式(YYYYMMDD)以及中文等判断: 在做sql数据的正确性审核中,需要判断数据是否满足日期格式,网上找不到相关的资料,于是自己花了半天写了一个简单的函数 具体 ...