合作网络(Corporative Network )并查集+路径压缩
#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 )并查集+路径压缩的更多相关文章
- 并查集+路径压缩(poj1988)
http://poj.org/problem?id=1988 Cube Stacking Time Limit: 2000MS Memory Limit: 30000K Total Submiss ...
- hdu 1558 线段相交+并查集路径压缩
Segment set Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 【数轴涂色+并查集路径压缩+加速】C. String Reconstruction
http://codeforces.com/contest/828/problem/C [题意] [思路] 因为题目保证一定有解,所有优化时间复杂度的关键就是不要重复染色,所以我们可以用并查集维护区间 ...
- 并查集 + 路径压缩(经典) UVALive 3027 Corporative Network
Corporative Network Problem's Link Mean: 有n个结点,一开始所有结点都是相互独立的,有两种操作: I u v:把v设为u的父节点,edge(u,v)的距离为ab ...
- [LA] 3027 - Corporative Network [并查集]
A very big corporation is developing its corporative network. In the beginning each of the N enterpr ...
- LA 3027 Corporative Network 并查集记录点到根的距离
Corporative Network Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu [S ...
- HDOJ 3635 并查集- 路径压缩,带秩合并
思路来源:http://blog.csdn.net/niushuai666/article/details/6990421 题目大意: 初始时,有n个龙珠,编号从1到n,分别对应的放在编号从1到n的城 ...
- LA 并查集路径压缩
题目大意:有n个节点,初始时每个节点的父亲节点都不存在.有两种操作 I u v:把点节点u的父亲节点设为v,距离为|u-v|除以1000的余数.输入保证执行指令前u没有父亲节点. E u:询问u到根节 ...
- snnu(1110) 传输网络 (并查集+路径压缩+离线操作 || 线段树)
1110: 传输网络 Time Limit: 3 Sec Memory Limit: 512 MBSubmit: 43 Solved: 18[Submit][Status][Web Board] ...
随机推荐
- centos6.3升级python至2.7.5
centos6.3自带的python版本是2.6.6,有时候需要升级到2.7.这里记录一下升级过程,方便查阅.实际上是转载自http://flyingdutchman.iteye.com/blog/1 ...
- CustomView
https://github.com/eltld/CustomView
- delete from inner join
Update Update XXX set XXX where 这种写法大家肯定都知道,才发现update和delete居然支持inner join的update方式,这个在表间关联来做更新和删除操作 ...
- 修复Xcode升级错误 — PCH File Error
http://www.rockia.net/2013/03/fix-xcode-update-pch-file-error Error:PCH File Built From A Different ...
- 减肥 day1
今天是我减肥第一天,现在体重是147斤, 早晨吃了一碗面,喝了一碗奶,中午吃了一个apple. 6点钟去打篮球,晚上去食堂稍微吃一点东西.
- Hadoop提供的reduce函数中Iterable 接口只能遍历一次的问题
今天在写MapReduce中的reduce函数时,碰到个问题,特此记录一下: void reduce(key, Iterable<*>values,...) { for(* v:value ...
- InspectIT_EUM 实现原理概述
在Git上查看 InspectIT 实现原理概述: 实现原理详解: 1.jsAgent如何注入到浏览器 通过ASM框架修改HttpService.service()方法,加入相关逻辑,对每一个Htt ...
- Spark SQL is a Spark module for structured data processing.
http://spark.apache.org/docs/latest/sql-programming-guide.html
- OPENCV在ARM平台的移植
两篇别人推荐给我的文章,我想直接复制过来,呵呵,但一想真不好,等我做一遍了再来写一遍.还是贴链接. OpenCV在ARM上的移植:http://www.cnblogs.com/emouse/archi ...
- NSString类的方法实现
创建一个新字符串并将其设置为 path 指定的文件的内容,使用字符编码enc,在error上返回错误 + (id)stringWithContentsOfURL:(NSURL *)url encodi ...