UVALive - 3027:Corporative Network
加权并查集
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define MAXN 20000+10
#define pii pair<int,int>
using namespace std;
int fa[MAXN];
int d[MAXN];
int n;
int Abs(int x){
return (x>?x:-x);
}
pii find(int x){
if(fa[x]!=x){
pii t=find(fa[x]);
fa[x]=t.first;
d[x]+=t.second;
}
return make_pair(fa[x],d[x]);
}
void solve(){
scanf("%d",&n);
for(int i=;i<=n;i++){
fa[i]=i,d[i]=;
}
char ch[]={};
while(scanf("%s",ch)&&ch[]!='O'){
if(ch[]=='E'){
int x;
scanf("%d",&x);
printf("%d\n",find(x).second);
}
else{
int x,y;
scanf("%d%d",&x,&y);
fa[x]=y;
d[x]=Abs(x-y)%;
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
solve();
}
return ;
}
UVALive - 3027:Corporative Network的更多相关文章
- 【暑假】[实用数据结构]UVAlive 3027 Corporative Network
UVAlive 3027 Corporative Network 题目: Corporative Network Time Limit: 3000MS Memory Limit: 30000K ...
- UVALive 3027 Corporative Network 带权并查集
Corporative Network A very big corporation is developing its corporative networ ...
- 并查集 + 路径压缩(经典) UVALive 3027 Corporative Network
Corporative Network Problem's Link Mean: 有n个结点,一开始所有结点都是相互独立的,有两种操作: I u v:把v设为u的父节点,edge(u,v)的距离为ab ...
- UVALive 3027 Corporative Network
---恢复内容开始--- Corporative Network Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld ...
- 3027 - Corporative Network(并差集)
3027 - Corporative Network A very big corporation is developing its corporative network. In the begi ...
- 3027 - Corporative Network
3027 - Corporative Network 思路:并查集: cost记录当前点到根节点的距离,每次合并时路径压缩将cost更新. 1 #include<stdio.h> 2 #i ...
- [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 ...
- 二分+最短路 uvalive 3270 Simplified GSM Network(推荐)
// 二分+最短路 uvalive 3270 Simplified GSM Network(推荐) // 题意:已知B(1≤B≤50)个信号站和C(1≤C≤50)座城市的坐标,坐标的绝对值不大于100 ...
随机推荐
- Beta Scrum
听说 Beta Scrum Day 1
- fs检测文件夹状态
var http = require("http"); var fs = require("fs"); var server = http.createServ ...
- python脚本,计算起点终点高程
import arcpy >>> import arcpy ... gd="D:/项目/shp/Pipe.gdb/ZK/GDPOINT" ... gx=" ...
- Linux--初次体验
关于Linux已经听闻很久的大名了,但是一直没有机会来使用,这次趁着放假的机会,来体验一把Linux吧. 之前使用visuabox和Ubuntu16,但是虚拟机总是不能连接互联网,在虚拟机上面无法上网 ...
- Java NIO之选择器
1.简介 前面的文章说了缓冲区,说了通道,本文就来说说 NIO 中另一个重要的实现,即选择器 Selector.在更早的文章中,我简述了几种 IO 模型.如果大家看过之前的文章,并动手写过代码的话.再 ...
- SpringBoot+Angular2 开发环境搭建
https://segmentfault.com/a/1190000007921675
- SpringCloud用户自定义配置信息的定义和查看
一.概念 在SpringCloud项目中,用户自己定义的配置信息也可以放在application.*,需要以 info打头,以便使用公用基础设施 /info 查看! 本文讲解基于 ConfigServ ...
- GIT入门笔记(20)- git 开发提交代码过程梳理
git开发提交流程新项目开发,可以直接往master上提交老项目维护,可以在分支上修改提交,多次add和commit之后,也可以用pull合并主干和本地master,解决冲突后再push 1.检出代码 ...
- 我的第二个开源库SuperTextView——中文文档
一个简单的TextView实现了打字机的效果让文字一个个显示出来, 方法介绍: startShow 开始打字 使用: startShow(int typeStartTime,int typeTime ...
- 【WebGL入门】画一个旋转的cube
最近搜罗了各种资料,发现WebGL中文网特别好用,很适合新手入门:http://www.hewebgl.com/article/getarticle/50 只需要下载好需要的所有包,然后用notepa ...