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. Linux 定时任务 Crontab按秒执行

    目前在crontab中最小执行时间单位为分钟. 如果需要按秒来执行,有以下两种方法: 方法一:通过sleep来实现 例: 1.创建test.php文件,这里测试通过打印时间好区分. <?php ...

  2. NVL和NVL2有什么区别,NULLIF 的使用.

    NULL指的是空值,或者非法值. NVL (expr1, expr2):expr1为NULL,返回expr2:不为NULL,返回expr1.注意两者的类型要一致 NVL2 (expr1, expr2, ...

  3. awk杂集-20170911

    awk 格式 1.awk -F '分割符' 'BEGIN{} /执行条件/{} END{}' filepath; 默认使用空格分割 2.awk -v word=$command '{print wor ...

  4. 错误信息:getOutputStream() has already been called for this response

    原因(转): getOutputStream()和getWriter()这两个方法不能在一个请求内同时使用, 如果使用forward,这时将要跳转到的页面是要用getWriter()方法获得输出流把页 ...

  5. 实现model中的文件上传FTP(二)

    上一篇博客记录了如何将model中的图片存入FTP,通过一个第三方的storages简单的实现了,但是后续我发现如果想在浏览器通过url直接获取图片,就不太容易了(大神轻喷,小弟自学django和py ...

  6. Promise API 简介

    Promise API 简介 译者注: 到处是回调函数,代码非常臃肿难看, Promise 主要用来解决这种编程方式, 将某些代码封装于内部. Promise 直译为"承诺",但一 ...

  7. Django学习案例一(blog):一. 创建project、app

    1.创建project 方法1:使用命令行创建项目.在E盘cmd执行如下命令: django-admin.py startproject myblog 方法2:使用pycharm创建项目.放置位置为D ...

  8. 利用a链接发送电子邮件

    实例代码: <a href="mailto:name1@rapidtables.com?cc=name2@rapidtables.com&subject=你好%20我是&quo ...

  9. javascript学习中自己对作用域和作用域链理解

    在javascript学习中作用域和作用域链还是相对难理解些,下面我关于javascript作用域和作用域链做一下详细介绍,给各位初学者答疑解惑. 首先我们介绍一下什么是作用域?  从字面上理解就是起 ...

  10. hdu3926 Hand in Hand 判断同构

    因为每个人小朋友只有两只手,所以每个点最多只有2度.图有可能是环.链,以及环和链构成的复杂图. 如何判断两幅图是否相似呢?判断相似是判断两幅图的圈的数量,以及构成圈的点数是否相同.还有判断链的数目和构 ...