Corporative Network
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 3664   Accepted: 1326

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
题意:有n个点,一开始每个点以自己为信号终点,当输入I x y,就连接x,y(len[x,y] =|x-y|%1000),并且X的信号终点指向Y,E x是输出x距离信号终点的距离,输O结束当前输入(题目好像有点
问题,N说要大于等于5,测试用例给的是4) 这题和我上面那篇博客一样的都是利用递归更新权值。值得注意的是这题询问的时候不用去寻找两个点的根结点,因为他们本来就是相邻结点了(父子关系)。
import java.util.Scanner;

public class Main {
final static int MAXSIZE = 20005;
static int[] father;
static int[] len; public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int tcase = sc.nextInt();
while (tcase-- > 0) {
int n = sc.nextInt();
father = new int[n+2];
len = new int[n+2];
for (int i = 1; i <= n; i++) {
father[i] = i;
len[i] = 0;
}
while (true) {
String str = sc.next();
if (str.equals("E")) {
int a = sc.nextInt();
find(a);
System.out.println(len[a]);
} else if(str.equals("I")){
int a = sc.nextInt();
int b = sc.nextInt();
father[a] = b;
len[a]=Math.abs(a-b)%1000;
} else break;
}
}
} private static int find(int a) {
if (a != father[a]) {
int temp = find(father[a]);
len[a] += len[father[a]];
father[a] = temp;
}
return father[a];
}
}

poj 1962(并查集+带权更新)的更多相关文章

  1. 浅谈并查集&种类并查集&带权并查集

    并查集&种类并查集&带权并查集 前言: 因为是学习记录,所以知识讲解+例题推荐+练习题解都是放在一起的qvq 目录 并查集基础知识 并查集基础题目 种类并查集知识 种类并查集题目 并查 ...

  2. POJ 1182 食物链 [并查集 带权并查集 开拓思路]

    传送门 P - 食物链 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit  ...

  3. 种类并查集——带权并查集——POJ1182;HDU3038

    POJ1182 HDU3038 这两个题比较像(一类题目),属于带权(种类)并查集 poj1182描绘得三种动物种类的关系,按照他一开始给你的关系,优化你的种类关系网络,最后看看再优化的过程中有几处矛 ...

  4. Poj1182 食物链(并查集/带权并查集)

    题面 Poj 题解 这里采用并查集的补集. \(x\)表示同类集合,\(x+n\)表示敌人集合,\(x+n\times2\)表示敌人的敌人集合. 如果当前给出的是一对同类关系,就判断\(x\)是否吃\ ...

  5. HDU 3038 How Many Answers Are Wrong 并查集带权路径压缩

    思路跟 LA 6187 完全一样. 我是乍一看没反应过来这是个并查集,知道之后就好做了. d[i]代表节点 i 到根节点的距离,即每次的sum. #include <cstdio> #in ...

  6. 【并查集&&带权并查集】BZOJ3296&&POJ1182

    bzoj1529[POI2005]ska Piggy banks [题目大意] n头奶牛m种语言,每种奶牛分别掌握一些语言.问至少再让奶牛多学多少种语言,才能使得它们能够直接或间接交流? [思路] ( ...

  7. POJ 1988 Cube Stacking( 带权并查集 )*

    POJ 1988 Cube Stacking( 带权并查集 ) 非常棒的一道题!借鉴"找回失去的"博客 链接:传送门 题意: P次查询,每次查询有两种: M x y 将包含x的集合 ...

  8. POJ 2195 Going Home (带权二分图匹配)

    POJ 2195 Going Home (带权二分图匹配) Description On a grid map there are n little men and n houses. In each ...

  9. poj 1984 并查集

    题目意思是一个图中,只有上下左右四个方向的边.给出这样的一些边, 求任意指定的2个节点之间的距离. 就是看不懂,怎么破 /* POJ 1984 并查集 */ #include <stdio.h& ...

随机推荐

  1. bzoj 3673&3674 可持久化并查集&加强版(可持久化线段树+启发式合并)

    CCZ在2015年8月25日也就是初三暑假要结束的时候就已经能切这种题了%%% 学习了另一种启发式合并的方法,按秩合并,也就是按树的深度合并,实际上是和按树的大小一个道理,但是感觉(至少在这题上)更好 ...

  2. 【队列】【P2827】【NOIP2016D2T3】蚯蚓

    传送门 Description 本题中,我们将用符号 $\lfloor c \rfloor$ 表示对 $c$ 向下取整,例如:$\lfloor 3.0 \rfloor = \lfloor 3.1 \r ...

  3. mybatis的模糊查询写法

    mybatis做like模糊查询   1.  参数中直接加入%% param.setUsername("%CD%");      param.setPassword("% ...

  4. session 超时设置

    Java Web开发Session超时设置 博客分类: Java Web 在Java Web开发中,Session为我们提供了很多方便,Session是由浏览器和服务器之间维护的.Session超时理 ...

  5. 同一台服务器(电脑)运行多个Tomcat

    同一台电脑运行不能同时运行多个未修改过配置tomcat的原因在于:一台电脑的一个端口只能被一个程序使用,多个tomcat启动会因为端口号号被占用的原因而启动失败. 如果想要在一台电脑上同时运行多个to ...

  6. JS常见的算法

    原文链接:Jack Pu's Blog 虽说我们很多时候前端很少有机会接触到算法.实际上学习数据结构与算法对于工程师去理解和分析问题都是有帮助的.如果将来当我们面对较为复杂的问题,这些基础知识的积累可 ...

  7. CSS hack浏览器兼容一览表

    CSS hack是指我们为了兼容各浏览器,而使用的特别的css定义技巧.这是国外摘来的一张CSS hack列表,显示了各浏览器对css hack的支持程度,对我们制作兼容网页非常有帮助.

  8. OScached页面缓存的入门使用

    OSCache的使用: 一,环境的搭建: 1,把oscache.jar file放在 /WEB-INF/lib 目录下(Put the oscache.jar file in the /WEB-INF ...

  9. 在此位置打开CMD

    Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\folder\shell\cmd]@="在此位置打开CMD"[HKE ...

  10. 【hdu1828/poj1177】线段树求矩形周长并

    题意如图 题解:这题非常类似与矩形面积并,也是维护一个被覆盖了一次以上的线段总长. 但是周长要算新出现的,所以每次都要和上一次做差求绝对值. x轴做一遍,y轴做一遍. 但是有个问题:矩形边界重合的时候 ...