LA3027 合作网络-并查集压缩路径
有N个结点
一次 I u v 操作表示把结点u的父结点设为v,距离为|u-v|%1000.输入保证执行指令前u没有父结点
一次E u 操作表示询问u到根结点的距离
O操作表示结束
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
;
int T,n,level,d[maxn],fa[maxn];
void init()
{
memset(d,,sizeof(d));
;i<=n;i++) fa[i]=i;
}
int find(int x){
if(fa[x]==x) return x;
int ans=find(fa[x]);
level+=d[x];
d[x]=level;
fa[x]=ans;
return ans;
}
int main()
{
char op;
cin>>T;
while(T--){
cin>>n;
init();
while(cin>>op){
if(op=='O') break;
else if(op=='I'){
int u,v;
cin>>u>>v;
d[u]=abs(u-v)%,fa[u]=v;
}else if(op=='E'){
int x;cin>>x;
if(fa[x]==x){
cout<<<<endl;continue;
}
level=;
find(x);
cout<<d[x]<<endl;
}
}
}
;
}
LA3027 合作网络-并查集压缩路径的更多相关文章
- POJ3728The merchant (倍增)(LCA)(DP)(经典)(||并查集压缩路径?)
There are N cities in a country, and there is one and only one simple path between each pair of citi ...
- poj1703Find them, Catch them(并查集以及路径压缩)
/* 题目大意:有两个不同的黑帮,开始的时候不清楚每个人是属于哪个的! 执行两个操作 A a, b回答a, b两个人是否在同一帮派,或者不确定 D a, b表示a, b两个人不在同一个帮派 思路:利用 ...
- [HDOJ2818]Building Block(带权并查集,路径压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2818 题意:有N个块,每次有两个操作: M x y表示把x所在的那一堆全部移到y所在的那一堆的下方. ...
- [HDOJ3635]Dragon Balls(并查集,路径压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3635 题意:有n个龙珠,n个城市.初始状态第i个龙珠在第i个城市里.接下来有两个操作: T A B:把 ...
- 关于并查集的路径压缩(Path Compress)优化
之前在CSDN看到一篇很受欢迎的讲解并查集的博文,其中自然用到了路径压缩: int pre[1000]; int find(int x){ int root = x; while(pre[root]! ...
- BZOJ 3674 可持久化并查集加强版(路径压缩版本)
/* bzoj 3674: 可持久化并查集加强版 http://www.lydsy.com/JudgeOnline/problem.php?id=3674 用可持久化线段树维护可持久化数组从而实现可持 ...
- LA 3027 合作网络 并查集
题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...
- poj1456Supermarket——并查集压缩查找
题目:http://poj.org/problem?id=1456 排序+贪心,每次选利润最大的,放在可能的最靠后的日期卖出,利用并查集快速找到下一个符合的日期. 代码如下: #include< ...
- codeforces #541 F Asya And Kittens(并查集+输出路径)
F. Asya And Kittens Asya loves animals very much. Recently, she purchased nn kittens, enumerated the ...
随机推荐
- Env: Linux下Source Insight安装
1.Wine安装 sudo apt-get install wine 如果有错误,可以sudo apt-get update 2.下载source insight,注意要是安装版 http://www ...
- 黄聪:C#如何操作JSON数据(读取、分析)
使用开源的类库Newtonsoft.Json(下载地址http://json.codeplex.com/).下载后加入工程就能用.通常可以使用JObject, JsonReader, JsonWrit ...
- 黄聪:wordpress更新失败‘C:\Windows\TEMP/wordpress.tmp’,更换临时保存路径的解决办法
1.如果可劲进入远程桌面,则给C:\WINDOWS\TEMP目录设置IIS访问权限. 2.虚拟机的方法:首先用FTP软件在网页空间wp-content目录中新建一个[tmp]目录,然后在wp-conf ...
- communication ports in DOS systems:
: CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7,COM8, COM9, LPT1, LPT2, LPT3, LPT4, L ...
- SOA_环境安装系列3_Oracle Weblogic安装和环境搭建(案例)
2014-01-03 Created By BaoXinjian
- 【Java】图片高质量缩放类
package com.test; import com.sun.image.codec.jpeg.JPEGImageEncoder; import com.sun.image.codec.jpeg. ...
- gerrit 配置 apache2 反向代理(转载)
Apache 2 Configuration To run Gerrit behind an Apache server using mod_proxy, enable the necessary A ...
- iOS 数字字符串的直接运算 + - * /
NSDecimalNumber *d1 = [NSDecimalNumber decimalNumberWithString:@"3.14"]; NSDecimalNumber * ...
- 在VMware虚拟机中配置DOS汇编开发环境!!
操作系统:win7 32位 DOS环境:DosBox 下载:http://www.dosbox.com/ 选择当前适合自己版本,下载就可以了. 汇编编译器:MASM 5.0 下载:http://do ...
- Enumerator yielder.yield 与 Proc.yield 区别
最近看ruby cookbook遇到这个用法,google一下,这里原文解释 http://stackoverflow.com/questions/18865860/enumerator-yielde ...