#include <iostream>
#include <algorithm>
#include <string>
using namespace std; const int maxn = + ;
int fa[maxn];
int d[maxn]; int Find(int x){
if (fa[x] != x){
int root = Find(fa[x]);
d[x] += d[fa[x]]; //路径压缩维护d[i]
return fa[x] = root;
}
else
return x;
} int main(){
/*
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
*/
int t;
cin >> t;
while (t--){
int n;
cin >> n; //init
for (int i = ; i <= n; i++){
fa[i] = i;
d[i] = ;
} string s;
while (cin >> s){
if (s[] == 'O')
break;
if (s[] == 'I'){
int a, b;
cin >> a >> b;
fa[a] = b;
d[a] = abs(a - b) % ;
}
else{
int c;
cin >> c;
Find(c);
cout << d[c] << endl;
}
}
}
/*
fclose(stdin);
fclose(stdout);
*/
return ;
}

合作网络(Corporative Network )并查集+路径压缩的更多相关文章

  1. 并查集+路径压缩(poj1988)

    http://poj.org/problem?id=1988 Cube Stacking Time Limit: 2000MS   Memory Limit: 30000K Total Submiss ...

  2. hdu 1558 线段相交+并查集路径压缩

    Segment set Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. 【数轴涂色+并查集路径压缩+加速】C. String Reconstruction

    http://codeforces.com/contest/828/problem/C [题意] [思路] 因为题目保证一定有解,所有优化时间复杂度的关键就是不要重复染色,所以我们可以用并查集维护区间 ...

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

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

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

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

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

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

  7. HDOJ 3635 并查集- 路径压缩,带秩合并

    思路来源:http://blog.csdn.net/niushuai666/article/details/6990421 题目大意: 初始时,有n个龙珠,编号从1到n,分别对应的放在编号从1到n的城 ...

  8. LA 并查集路径压缩

    题目大意:有n个节点,初始时每个节点的父亲节点都不存在.有两种操作 I u v:把点节点u的父亲节点设为v,距离为|u-v|除以1000的余数.输入保证执行指令前u没有父亲节点. E u:询问u到根节 ...

  9. snnu(1110) 传输网络 (并查集+路径压缩+离线操作 || 线段树)

    1110: 传输网络 Time Limit: 3 Sec  Memory Limit: 512 MBSubmit: 43  Solved: 18[Submit][Status][Web Board] ...

随机推荐

  1. Deep Learning 31: 不同版本的keras,对同样的代码,得到不同结果的原因总结

    一.疑问 这几天一直纠结于一个问题: 同样的代码,为什么在keras的0.3.3版本中,拟合得比较好,也没有过拟合,验证集准确率一直高于训练准确率. 但是在换到keras的1.2.0版本中的时候,就过 ...

  2. C# 事件处理与自定义事件

    http://blog.csdn.net/cyp403/article/details/1514023 图一                                               ...

  3. myeclipse -vmargs -Xmx512m -XX:MaxPermSize=256m -XX:ReservedCodeCacheSize=64m

    myeclipse.ini把里面的参数为 -vmargs -Xmx512m -XX:MaxPermSize=256m -XX:ReservedCodeCacheSize=64m 以对于我而言,我只要把 ...

  4. spring cloud 启动报错-must be declared as an @AliasFor [serviceId], not [name].

    项目加入FeignClient后再启动就报错,具体报错信息如下: org.springframework.core.annotation.AnnotationConfigurationExceptio ...

  5. 传统maven项目创建

    转自:https://blog.csdn.net/wangfengtong/article/details/77098238 需求表均同springmvc案例 此处只是使用maven 注意,以下所有需 ...

  6. vfork函数的使用【学习笔记】

    #include "apue.h" ; int main(void) { int var; pid_t pid; ; printf("before vfork\r\n&q ...

  7. 树状数组(二叉索引树 BIT Fenwick树) *【一维基础模板】(查询区间和+修改更新)

    刘汝佳:<训练指南>Page(194) #include <stdio.h> #include <string.h> #include <stdlib.h&g ...

  8. CoreData兼容iOS9和iOS10

    由于iOS10之后CoreData Stack的更改无法在iOS9的系统中运行,所以我们需要对上一小节中封装的工具类进行系统版本的兼容 iOS9和iOS10中CoreData最本质的区别其实就是管理对 ...

  9. hdu acm 1114 Piggy-Bank 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 题目意思:给出一个空的猪仔钱ang 的重量E,和一个装满钱的猪仔钱ang 的重量F你,实质上能装 ...

  10. Silverlight中使用MVVM(2)

    Silverlight中使用MVVM(1)--基础 Silverlight中使用MVVM(2)—提高 Silverlight中使用MVVM(3)—进阶 Silverlight中使用MVVM(4)—演练 ...