合作网络(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] ...
随机推荐
- 一篇很好的讲解SIFT算法的文章
http://blog.csdn.net/zddblog/article/details/7521424
- java:BufferedImage推断图像通道顺序并转RGB/BGR
一般来说java ImageIO处理读取图像时.通常是RGB或ARGB格式,可是有的时候.我们须要图像是BGR格式. 比方通过JNI将图像矩阵传递给动态库,动态库里用OpenCV来处理矩阵,而用Ope ...
- UsbManager, UsbDevice的简单示例
activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...
- Android Studio解决导入项目非常慢
Android Studio比Eclipse ADT有巨大的优势.Android Studio原生支持使用Gradle来构建项目,使用动态语言Groovy定义项目构建的过程,避免了build.xml文 ...
- [RK3288][Android6.0] 调试笔记 --- 系统识别不同硬件版本方法【转】
本文转载自:http://m.blog.csdn.net/kris_fei/article/details/70226451 Platform: RockchipOS: Android 6.0Kern ...
- HDU2102 A计划 —— BFS
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 A计划 Time Limit: 3000/1000 MS (Java/Others) Me ...
- 解决VMware安装Ubuntu的过程中窗口过小无法看到【下一步】按钮的问题
只要按住ALT键向上拖动窗口
- MySQL性能优化-I/O相关配置参数
本文介绍InnoDB和MyISAM两种存储引擎的I/O相关参数配置. 1.InnoDB I/O相关配置 Innodb是一种事务型的存储引擎,为了减少提交事务时产生的io开销,innodb采用了写日志 ...
- Android gif 录屏
/********************************************************************************** * Android gif 录屏 ...
- kafka实时流数据架构
初识kafka https://www.cnblogs.com/wenBlog/p/9550039.html 简介 Kafka经常用于实时流数据架构,用于提供实时分析.本篇将会简单介绍kafka以及它 ...