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 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 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 题解:并查集。注意:由于本题的合并操作指定了父节点和子节点,所以不能使用启发式合并。不使用路径压缩能AC。若使用路径压缩,需要在压缩时维护每个节点到父节点的距离d[i],合并时更新。 代码:
 #include<stdio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
#include<stdlib.h>
#include<stdbool.h> #define rep(i,a,b) for(i=(a);i<=(b);i++)
#define clr(x,y) memset(x,y,sizeof(x))
#define sqr(x) (x*x) int i,j,n,
father[],d[]; void pre()
{
clr(father,);
clr(d,);
} int find(int x)
{
int r,k,p,sum=; k=x;
while(k!=father[k]) {
sum+=abs(k-father[k])%;
k=father[k];
} return sum;
} int main()
{
int i,T,n,x,y,f1;
char ch;
scanf("%d",&T);
while(T--) {
pre();
scanf("%d",&n);
rep(i,,n) father[i]=i; while(true) {
scanf("%c",&ch);
if(ch=='O') break;
if(ch=='E') {
scanf("%d",&x);
f1=find(x);
printf("%d\n",f1);
}
if(ch=='I') {
scanf("%d%d",&x,&y);
father[x]=y;
} } } return ;
}

 

[LA] 3027 - Corporative Network [并查集]的更多相关文章

  1. LA 3027 Corporative Network 并查集记录点到根的距离

    Corporative Network Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [S ...

  2. UVALive - 3027 Corporative Network (并查集)

    这题比较简单,注意路径压缩即可. AC代码 //#define LOCAL #include <stdio.h> #include <algorithm> using name ...

  3. 【LA 3027 Corporative Network】

    ·一些很可爱的询问和修改,放松地去用并查集解决. ·英文题,述大意: 输入n(5<=n<=20000)表示树有n个节点,并且会EOF结束地读入不超过 20000个操作,一共有两种:    ...

  4. 并查集(路径更新) LA 3027 Corporative Network

    题目传送门 题意:训练指南P192 分析:主要就是一个在路径压缩的过程中,更新点i到根的距离 #include <bits/stdc++.h> using namespace std; c ...

  5. LA 3027 Corporative Network

    这题感觉和 POJ 1988 Cube Stacking 很像,在路径压缩的同时递归出来的时候跟新distant数组 我发现我一直WA的原因是,命令结束是以字母o结束的,而不是数字0!! //#def ...

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

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

  7. 3027 - Corporative Network

    3027 - Corporative Network 思路:并查集: cost记录当前点到根节点的距离,每次合并时路径压缩将cost更新. 1 #include<stdio.h> 2 #i ...

  8. 3027 - Corporative Network(并差集)

    3027 - Corporative Network A very big corporation is developing its corporative network. In the begi ...

  9. UVALive 3027 Corporative Network 带权并查集

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

随机推荐

  1. 渐变颜色的进度条WGradientProgress-备用

    今天我们来实现一个iOS平台上的进度条(progress bar or progress view).这种进度条比APPLE自带的更加漂亮,更加有“B格”.它拥有渐变的颜色,而且这种颜色是动态移动的, ...

  2. js运算符的优先级

    自上向下优先级降低 运算符 描述 . [] () 字段访问.数组下标.函数调用以及表达式分组 ++ -- - ~ ! delete new typeof void 一元运算符.返回数据类型.对象创建. ...

  3. Codeforces 437E The Child and Polygon

    http://codeforces.com/problemset/problem/437/E 题意:求一个多边形划分成三角形的方案数 思路:区间dp,每次转移只从一个方向转移(L,R连线的某一侧),能 ...

  4. QT下实现对Linux Shell调用的几种方法

    使用QProcess QThread ============================================ #include <QProcess>int main(){ ...

  5. Resty 一款极简的restful轻量级的web框架

    https://github.com/Dreampie/Resty Resty 一款极简的restful轻量级的web框架 开发文档 如果你还不是很了解restful,或者认为restful只是一种规 ...

  6. mv command:unable to remove target: Is a director

    mv command:unable to remove target: Is a director This is somewhat simple as long as we understand t ...

  7. 黑马程序员_Java_多线程

    8.多线程 8.1.多线程概述 进程:是一个正在执行中的程序.每一个进程执行都有一个执行顺序.该顺序是一个执行路径,或者叫一个控制单元. 线程(例:FlashGet):就是进程中一个独立的控制单元.线 ...

  8. css中的7中属性选择器

    在CSS的选择符中有七个属性选择符.它们分别是: 1.E[att] 选择具有att属性的E元素. 2.E[att="val"] 选择具有att属性且属性值等于val的E元素. 3. ...

  9. TPCC-UVA测试环境搭建与结果分析

    一.    准备 操作系统 :Linux, 内核版本2.6 需要软件:tpccuva-1.2.3, postgresql-8.1.15, gnuplot-4.2.5. tccuva是实现标准TPC-C ...

  10. 解决linux不能使用chmod更改权限的问题

    本人安装的是win10和ubuntu的双系统,发现在ubuntu下挂载windows硬盘不用命令chmod更改文件的权限,解决方法记录如下: 对于使用命令$ chmod 777 dirname更改不了 ...