合作网络(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] ...
随机推荐
- EF中 Code-First 方式的数据库迁移
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/code-first-migrations-with-entity-framework/ 系列目 ...
- 2016/05/27 php上传文件常见问题总结
php上传文件常见问题总结 投稿:hebedich 字体:[增加 减小] 类型:转载 时间:2015-02-03我要评论 这篇文章主要介绍了php上传文件常见问题总结,基本上经常碰到的问题的处理都列了 ...
- Lambda 闭包 匿名 函数 类
深入理解Java 8 Lambda(语言篇——lambda,方法引用,目标类型和默认方法) - _Luc_ - 博客园 https://www.cnblogs.com/figure9/p/java-8 ...
- umask文件屏蔽字的使用【学习笔记】
#include "apue.h" #include <fcntl.h> #define RWRWRW (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP ...
- Dedecms(织梦)文章内容页和图片集内容页,调用缩略图的方法
Dedecms(织梦)文章内容页和图片集内容页,调用缩略图的方法,亲测可用! Dedecms(织梦)首页的图片调用,相信大家已经非常的清楚,但是今天我在进行内容页的编写的时候,发现了内容页图片的调用问 ...
- I.MX6 不一定要设置BOOT_MODE进入烧录模式
/************************************************************************* * I.MX6 不一定要设置BOOT_MODE进入 ...
- SPOJ:Strange Waca(不错的搜索&贪心&剪枝)
Waca loves maths,.. a lot. He always think that 1 is an unique number. After playing in hours, Waca ...
- [Selenium] 测试机器上安装了多个Firefox,如何指定运行哪一个?
可通过FirefoxBinary 来指定运行某个路径下的Firefox, 示例代码如下: public class testFirefoxBinary{ public static void main ...
- mack pro常用快捷键
fn + left / right / up / down 相当于 home/end/page up /page down delete 删除光标前一个字符 fn + delete 删除当前光标后一个 ...
- 【BZOJ 3884】 上帝与集合的正确用法
[题目链接] 点击打开链接 [算法] 通过欧拉拓展定理,列出递推公式 [代码] #include<bits/stdc++.h> using namespace std; typedef l ...