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


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. java.lang.Thread

    package java.lang; import java.lang.ref.Reference; import java.lang.ref.ReferenceQueue; import java. ...

  3. pandas 处理dataframe(一)

    1.读取csv文件 df = pd.read_csv('30lines.csv') 2.删除第一列 df.drop(df.columns[[0]].axis=1,inplace=True) 3. co ...

  4. 如何为form表单的button设置submit事件

    若button按钮没有type属性,浏览器默认按照type=submit逻辑处理,这样若将没有type的button放在form表单中,点击按钮就会执行form表单提交

  5. 小z的袜子

    传送门 题目描述 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命-- 具体来说,小Z把这N只袜子从 ...

  6. 赋值号和printf

    1.赋值号会自动转换类型: int a= 2.1; printf(‘‘%d’’,a);         //输出结果是2 2.printf不会转换类型,而是直接将内存中表示的补码数拿出来,最明显的就是 ...

  7. softmax_cross_entropy_with_logits

    softmax_cross_entropy_with_logits 原创文章,请勿转载 函数定义 def softmax_cross_entropy_with_logits(_sentinel=Non ...

  8. 创建分模块的maven项目

    折腾了我2天的maven,整理一下,以后做个参考 一.什么是maven项目: Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具. Maven ...

  9. JAVA并发编程学习笔记------FutureTask

    FutureTask是Future和Callable的结合体.传统的代码是这样写的Future f = executor.submit(new Callable()); 然后通过Future来取得计算 ...

  10. Effective Java 之 --- 用私有构造器或者枚举类型强化Singleton属性

    Singleton指仅仅被实例化一次的类,通常用来代表那些本质上唯一的系统组件,实现Singleton有三种方法: 1)公有静态成员是个final域,享有特权的用户可以调用AccessibleObje ...