合作网络(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] ...
随机推荐
- object-c iOS 教程 git for mac
本文转载至 http://blog.csdn.net/u011728347/article/details/10035191 http://rypress.com/tutorials/object ...
- 直播:中国HBase技术社区第一届MeetUp
6月6日,由中国HBase技术社区组织,阿里云主办的中国第一届HBase Meetup将在北京举行,来自阿里.小米.滴滴.360等公司的各位大神会共同探讨HBase2.0的技术革新,HBase在国内各 ...
- Mac 下如何安装pip 和xlwt
sudo easy_install pip pip install xlwt sudo pip install xlwt sudo pip install requests
- OO的片段,继承与组合,继承的优点与目的,虚机制在构造函数中不工作
摘自C++编程思想: ------------------------------ 继承与组合:接口的重用 ------------------------------- 继承和组合都允许由已存在的类 ...
- rails find find_by where
find根据id进行查询,像Product.find(3),查询语句是Product Load (0.1ms) SELECT "products".* FROM "pro ...
- ES6 中的let 声明变量
1.let是声明的是块级变量,不会污染全局,一般条件与循环中会用到: 2.let 不可以变量提升: 3.let不遵循作用域,一个作用域内如果有该变量,就不会到全局去找,也不可以在一个作用域重复声明一 ...
- 初探linux子系统集之led子系统(二)【转】
本文转载自:http://blog.csdn.net/eastmoon502136/article/details/37606487 巴西世界杯,德国7比1东道主,那个惨不忍睹啊,早上起来看新闻,第一 ...
- ComboBox联动 (AJAX BS实现)
//从webservice中取数据ajax Ext.Ajax.request({ url: 'WebService.asmx/GetComboxFi ...
- 【opencv】opencv在图片、视频嵌中英文字符的方法
转自:http://www.cnblogs.com/hujingshuang/p/5119015.html 说明:本博文是根据前人已有的成果并结合自己的理解而成的.为了避免让读者感到繁琐,我将运用小学 ...
- 关于布局(Layout)的一切
之前在布局中有很多问题也有很多经验,遗憾都没记下来.现在一点点记下一些东西. 1.外层用LinearLayout的话,常常把orientation设成vertical, android:orienta ...