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 带权并查集的更多相关文章

  1. UVA 3027 Corporative Network 带权并查集、

    题意:一个企业要去收购一些公司把,使的每个企业之间互联,刚开始每个公司互相独立 给出n个公司,两种操作 E I:询问I到I它连接点最后一个公司的距离 I I J:将I公司指向J公司,也就是J公司是I公 ...

  2. POJ1962Corporative Network[带权并查集]

    Corporative Network Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 3945   Accepted: 14 ...

  3. POJ1962:Corporative Network【带权并查集】

    <题目链接> 题目大意: n个节点,若干次询问,I x y表示从x连一条边到y,权值为|x-y|%1000:E x表示询问x到x所指向的终点的距离.   解题分析: 与普通的带权并查集类似 ...

  4. 【poj 1962】Corporative Network(图论--带权并查集 模版题)

    P.S.我不想看英文原题的,但是看网上题解的题意看得我 炒鸡辛苦&一脸懵 +_+,打这模版题的代码也纠结至极了......不得已只能自己翻译了QwQ . 题意:有一个公司有N个企业,分成几个网 ...

  5. 【暑假】[实用数据结构]UVAlive 3027 Corporative Network

    UVAlive 3027 Corporative Network 题目:   Corporative Network Time Limit: 3000MS   Memory Limit: 30000K ...

  6. 【bzoj 1202】[HNOI2005] 狡猾的商人(图论--带权并查集+前缀和)

    题意:一个账本记录了N个月以来的收入情况,现在有一个侦探员不同时间偷看到M段时间内的总收入,问这个账本是否为假账. 解法:带权并查集+前缀和.   判断账本真假是通过之前可算到的答案与当前读入的值是否 ...

  7. 【poj 1988】Cube Stacking(图论--带权并查集)

    题意:有N个方块,M个操作{"C x":查询方块x上的方块数:"M x y":移动方块x所在的整个方块堆到方块y所在的整个方块堆之上}.输出相应的答案. 解法: ...

  8. 并查集——poj2236(带权并查集)

    题目:Wireless Network 题意:给定n台已损坏计算机的位置和计算机最远通信距离d,然后分别根据命令执行以下两种操作: "O p" (1 <= p <= N ...

  9. 带权并查集:HDU3172-Virtual Friends

    Virtual Friends Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...

随机推荐

  1. docker(三):Harbor 1.8.0 仓库的安装和使用

    回顾: docker(一):docker是什么? docker(二):CentOS安装docker docker(部署常见应用):docker部署mysql 安装的先决条件 硬件环境 1.CPU    ...

  2. Polyfill 与 Shim

    Polyfill 与 Shim polyfill 的概念是 Remy Sharp 在2010年提出的. polyfill,或 polyfiller ,表示为开发人员提供旧浏览器没有原生支持的较新功能的 ...

  3. python-sqlite3事务

    sqlite3事务总结: 在connect()中不传入 isolation_level 事务处理: 使用connection.commit() #!/usr/bin/env python # -*- ...

  4. Struts2的学习链接

    ---- Struts2的学习途径 (downpour) http://www.iteye.com/wiki/struts2/1306-struts2-way-of-learning ---- Str ...

  5. GEF中连接的实现

    在GEF绘图笔想象中复杂许多,需要很多组件的依赖与支持,稍微弄错一个引用,或一个操作调试起来就比较麻烦,下面列一下实现一个连接线功能所需要实现的类及添加的方法 建议大图查看. 相关代码:参考<G ...

  6. mac os x install redis-3.2.9

    下载.解压.重命名并且编译安装Redis~ wget http://download.redis.io/releases/redis-3.2.9.tar.gz ~ tar xzf redis-3.2. ...

  7. Win10 UWP Tile Generator

    图标生成器 https://marketplace.visualstudio.com/items?itemName=shenchauhan.UWPTileGenerator 备份地址: http:// ...

  8. Block Functionality

    Block Functionality A block is an anonymous inline collection of code that: Has a typed argument lis ...

  9. Python基础学习_01字符串的拼接(字符串的格式化输出)

    # 字符串的拼接 ---字符串的格式化输出 # 字符串的拼接 ---字符串的格式化输出 name = input("name:") age = input("age:&q ...

  10. 使用 Laravel 5.5+ 更好的来实现 404 响应

    译文首发于 使用 Laravel 5.5+ 更好的来实现 404 响应,转载请注明出处! Laravel 5.5.10 封装了两个有用的路由器方法,可以帮助我们为用户提供更好的 404 页面.现在,当 ...