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.
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]; } }