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

Description

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.

Input

Your program has to be ready to solve more than one test case. The first line of the input 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

Source


维护d[i]为到父节点到距离,路径压缩时维护
//
// main.cpp
// poj1962
//
// Created by Candy on 10/11/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N=1e5+;
typedef long long ll;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int T,n,u,v;
char s[];
int fa[N],d[N];
inline int find(int x){
if(x!=fa[x]){
int root=find(fa[x]);
d[x]+=d[fa[x]];
return fa[x]=root;
}else return x;
}
int main(int argc, const char * argv[]) {
T=read();
while(T--){
n=read();
for(int i=;i<=n;i++){fa[i]=i;d[i]=;}
while(true){
scanf("%s",s);
if(s[]=='O') break;
if(s[]=='I'){
u=read();v=read();
fa[u]=v;
d[u]=abs(u-v)%;
}else{
u=read();
find(u);
printf("%d\n",d[u]);
}
}
} return ;
}

POJ1962Corporative Network[带权并查集]的更多相关文章

  1. UVALive 3027 Corporative Network 带权并查集

                         Corporative Network A very big corporation is developing its corporative networ ...

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

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

  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. 并查集——poj2236(带权并查集)

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

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

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

  7. HDU_3172_带权并查集

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

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

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

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

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

随机推荐

  1. MySQL字符串替换与HTML转义

    ps:今天遇到一个问题,从数据库读取一个字符串,然后在jsp用EL表达式显示时,因为数据库原始数据是带有HTML标签的,所以显示的时候会把标签直接转换成HTML,但是我想要的是HTML标签字符串,所以 ...

  2. java web学习总结(十二) -------------------Session

    一.Session简单介绍 在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),注意:一个浏览器独占一个session对象(默认情况下).因此,在需要保存用户数据时,服务 ...

  3. mvc view 中 js 不反应

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  4. [JS]笔记12之事件机制--事件冒泡和捕获--事件监听--阻止事件传播

    -->事件冒泡和捕获-->事件监听-->阻止事件传播 一.事件冒泡和捕获 1.概念:当给子元素和父元素定义了相同的事件,比如都定义了onclick事件,点击子元素时,父元素的oncl ...

  5. Javascript对象

    这次的分享,主要还是想跟大家聊聊Javascript语言中很重要的概念之一,对象.为什么说之一呢?因为Javascript其他重要概念还包括:作用域 作用域链 继承 闭包 函数 继承 数组 ..... ...

  6. iOS 开源项目

    在 Github 上 Star 太多了,有时候很难找到自己想要的开源库,所以在此记录下来.便于自己开发使用,也顺便分享给大家. 动画 awesome-ios-animation收集了iOS平台下比较主 ...

  7. First,FirstOrDefault,Single,SingleOrDefault的区别

    操作符 如果源序列是空的 源序列只包含一个元素 源序列包含多个元素 First 抛异常 返回该元素 返回第一个元素 FirstOrDefault 返回default(TSource) 返回该元素 返回 ...

  8. XSS跨站测试代码大全

    '><script>alert(document.cookie)</script>='><script>alert(document.cookie)&l ...

  9. LINUX下C-C++类软件的诊断

    一般C/C++写程序由于内存.指针等问题在后期的过程都一般会遇到内存等资源泄露.崩溃等问题,对于这些问题,一般从哪个角度解决呢? 下面从几个角度进行总结: 内存泄露 首先需要注意一个问题是top或者h ...

  10. 自定义View字段表头

    适用场景: 三个列表进行Join,然后试图上显示ProjectedField,而ProjectedField不支持设置DisplayName.默认只能显示英文名. join caml如下: <V ...