UVALive 3027 Corporative Network 带权并查集
Corporative Network
A very big corporation is developing its corporative network. In the beginning each of the N enterprises
of the corporation, numerated from 1 to N, organized its own computing and telecommunication center.
Soon, for amelioration of the services, the corporation started to collect some enterprises in clusters,
each of them served by a single computing and telecommunication center as follow. The corporation
chose one of the existing centers I (serving the cluster A) and one of the enterprises J in some other
cluster B (not necessarily the center) and link them with telecommunication line. The length of the
line between the enterprises I and J is |I −J|(mod 1000). In such a way the two old clusters are joined
in a new cluster, served by the center of the old cluster B. Unfortunately after each join the sum of the
lengths of the lines linking an enterprise to its serving center could be changed and the end users would
like to know what is the new length. Write a program to keep trace of the changes in the organization
of the network that is able in each moment to answer the questions of the users.
Your program has to be ready to solve more than one test case.
Input
The first line of the input file will contains only the number T of the test cases. Each test will start
with the number N of enterprises (5 ≤ N ≤ 20000). Then some number of lines (no more than 200000)
will follow with one of the commands:
E I — asking the length of the path from the enterprise I to its serving center in the moment;
I I J — informing that the serving center I is linked to the enterprise J.
The test case finishes with a line containing the word ‘O’. The ‘I’ commands are less than N.
Output
The output should contain as many lines as the number of ‘E’ commands in all test cases with a single
number each — the asked sum of length of lines connecting the corresponding enterprise with its serving
center.
Sample Input
1
4
E 3
I 3 1
E 3
I 1 2
E 3
I 2 4
E 3
O
Sample Output
0
2
3
5
题意:
有n个节点,初始时每个节点的父节点都不存在,你的任务是执行一次I操作和E操作,格式如下:
I u v:把节点u的父节点设为v,距离为|u-v|除以1000的余数。输入保证之星指令前没有父节点
E u:询问u到根节点的距离
题解:
带权并查集,存好与祖先的距离,注意覆盖值就好了
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5+, M = , mod = 1e9 + , inf = 0x3f3f3f3f;
typedef long long ll;
int f[N],d[N],T,n,a,b;
char ch[N];
int finds(int x) {
if(f[x]!=x) {
int root = finds(f[x]);
d[x] += d[f[x]];
return f[x] = root;
}
return x;
}
int main() {
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
for(int i=;i<=n;i++) f[i] = i;
memset(d,,sizeof(d));
while(scanf("%s",ch)&&ch[]!='O') {
if(ch[]=='E') {
scanf("%d",&a);
int x = finds(a);
printf("%d\n",d[a]);
}
else {
scanf("%d%d",&a,&b);
f[a] = b;
d[a] += (abs(a-b)%);
}
}
}
return ;
}
UVALive 3027 Corporative Network 带权并查集的更多相关文章
- UVA 3027 Corporative Network 带权并查集、
题意:一个企业要去收购一些公司把,使的每个企业之间互联,刚开始每个公司互相独立 给出n个公司,两种操作 E I:询问I到I它连接点最后一个公司的距离 I I J:将I公司指向J公司,也就是J公司是I公 ...
- POJ1962Corporative Network[带权并查集]
Corporative Network Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 3945 Accepted: 14 ...
- POJ1962:Corporative Network【带权并查集】
<题目链接> 题目大意: n个节点,若干次询问,I x y表示从x连一条边到y,权值为|x-y|%1000:E x表示询问x到x所指向的终点的距离. 解题分析: 与普通的带权并查集类似 ...
- 【poj 1962】Corporative Network(图论--带权并查集 模版题)
P.S.我不想看英文原题的,但是看网上题解的题意看得我 炒鸡辛苦&一脸懵 +_+,打这模版题的代码也纠结至极了......不得已只能自己翻译了QwQ . 题意:有一个公司有N个企业,分成几个网 ...
- 【暑假】[实用数据结构]UVAlive 3027 Corporative Network
UVAlive 3027 Corporative Network 题目: Corporative Network Time Limit: 3000MS Memory Limit: 30000K ...
- 【bzoj 1202】[HNOI2005] 狡猾的商人(图论--带权并查集+前缀和)
题意:一个账本记录了N个月以来的收入情况,现在有一个侦探员不同时间偷看到M段时间内的总收入,问这个账本是否为假账. 解法:带权并查集+前缀和. 判断账本真假是通过之前可算到的答案与当前读入的值是否 ...
- 【poj 1988】Cube Stacking(图论--带权并查集)
题意:有N个方块,M个操作{"C x":查询方块x上的方块数:"M x y":移动方块x所在的整个方块堆到方块y所在的整个方块堆之上}.输出相应的答案. 解法: ...
- 并查集——poj2236(带权并查集)
题目:Wireless Network 题意:给定n台已损坏计算机的位置和计算机最远通信距离d,然后分别根据命令执行以下两种操作: "O p" (1 <= p <= N ...
- 带权并查集:HDU3172-Virtual Friends
Virtual Friends Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
随机推荐
- cloudfoundry service broker 制作
实验室这边需要制作service broker.从今天开始将精力投入其中.
- 禁止button标签提交form表单,变成普通按钮
button有个type属性,属性值可为button.submit.reset button=普通按钮,直接点击不会提交表单submit=提交按钮,点击后会提交表单reset=表单复位 当button ...
- Intel VTune Amplifier XE 使用
VTune <VTune 开发者手册> 1. 安装 1.1 软件安装 下载: (安装包下载地址) 安装: # 1.解压 tar -zxvf filename.tar.gz # 2.安装 c ...
- parseint和isNaN用法
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- Vs2010无法打开文件“Kernel32.lib”、无法打开“libcpmt.lib”"msvcprt.lib"
1.对于无法打开"Kernel"问题,即使复制lib文件到目录,仍然会出现最后的错误; 原因:WindowsSdk 安装失败! 方法:重装 microsoft SDK6.0 ,再在 ...
- Linux学习之路三:重要概念之Linux系统层次结构
上图来自Unix编程圣经<APUE>英文第二版.如图,处于最中心的是系统内核,负责机器硬件资源管理,进程管理等:shell,函数库(值得记住的是C标准函数库)和某些应用程序均直接构建于内核 ...
- -webkit-appearance: none; 去除浏览器默认样式
-webkit-appearance: none; 去除浏览器默认样式
- C++继承与组合
转自https://blog.csdn.net/caoyan_12727/article/details/52337297 类的组合和继承一样,是软件重用的重要方式.组合和继承都是有效地利用已有类的资 ...
- redis过期key删除
LZ一开始配置到启动类里面,结果出现了主线程阻塞的情况. 如下是流程: 首先修改配置文件redis.conf中的:notify-keyspace-events Ex,默认为notify-keyspac ...
- nyoj2-吝啬的国度
吝啬的国度 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 在一个吝啬的国度里有N个城市,这N个城市间只有N-1条路把这个N个城市连接起来.现在,Tom在第S号城市,他有 ...