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


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. js禁止开发者工具

    $(document).keydown(function() { return key(arguments[0]) }); function key(e) { //f12 var keynum; if ...

  2. Log4j扩展使用--输出地Appender

    OK,现在我们来研究输出低Appended. Appender控制日志输出的位置 Log4j日志系统允许把日志输出到不同的地方,如控制台(Console).文件(Files).根据天数或者文件大小产生 ...

  3. k8s 如何 Failover?- 每天5分钟玩转 Docker 容器技术(127)

    上一节我们有 3 个 nginx 副本分别运行在 k8s-node1 和 k8s-node2 上.现在模拟 k8s-node2 故障,关闭该节点. 等待一段时间,Kubernetes 会检查到 k8s ...

  4. Halcon一日一练:创建三通道图像

    首先理解一个什么是三通道图像: 三通道图像就是彩色图像,我们之前黑白相机或黑白电视机都是彩用的灰阶图像,即单通道图像,一般是2的8次方个灰阶,即256个灰阶.彩色图像采用RGB,红绿蓝三个通道来合成彩 ...

  5. linux服务器配置pyspark解决py4j报错等问题

    1.下载spark,python包 略 2.环境变量配置 打开 ~/.bashrc配置文件 如图添加下列环境变量及path 3.退出配置文件,输入 source ~/.bashrc 来执行你添加的一些 ...

  6. iOS-MD5加密、SHA1加密

    1.MD5加密 ///MD5加密************************************** + (NSString *)md5:(NSString *)inputString{ co ...

  7. Numpy基础学习

    Numpy(Numerical Python的简称)是高性能科学计算和数据分析的基础包. 主要的功能: 1.ndarray,一个具有矢量运算和复杂广播工能的快速且节省空间的多维数组 2.用于对整组数据 ...

  8. 用fiddler2调试localhost

    1.用 http://localhost. (locahost紧跟一个点号): 2.用 http://127.0.0.1. (127.0.0.1紧跟一个点号): 3.用 http://machinen ...

  9. Golang常用包

    fmt 实现了格式化IO函数,格式化短语派生于C io 提供了原始的io操作 bufio 这个包实现了缓冲的io,io.Reader 和 io.Write 对象 sort 对数组和用户定义集合的原始的 ...

  10. HDU 3595 GG and MM [Every-SG]

    传送门 题意: 两个数$x,y$,一个人的决策为让大数减去小数的任意倍数(结果不能为负),出现0的人胜 一堆这样的游戏同时玩 Every-SG 游戏规定,对于还没有结束的单一游戏,游戏者必须对该游戏进 ...