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 the 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 are | i−j| (mod 1000). In such a-clusters is 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's able in each moment to answer the questions of the users.
Your program have to is ready-to-solve more than one test case.
Input
The. RST line of the input Le would contains only the number T of the the test cases. Each test would start
With the number N of enterprises (5≤n≤20000). Then some number of lines (no more than 200000)
Would follow with one of the commands:
E i-asking The length of the path from the enterprise I to their serving center in the moment;
I I j-informing that the serving center I was linked to the Enterprise J.
The test case? Nishes with a line containing the word ' O '. The ' I ' commands is 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
Test instructions
There are n nodes, and the parent node of each node does not exist at the beginning, your task is to perform an I operation and an e operation, in the following format:
I U V: Sets the parent node of node u to V, the remainder of the distance is |u-v| divided by 1000. No parent node before entering the Guaranteed Star command
E u: Ask u to the distance of the root node
Exercises
Take the right and check the set, save the distance from the ancestors, pay attention to the value of the coverage is good
#include <iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>using namespacestd;Const intN = 1e5+Ten, M =30005, mod = 1e9 +7, INF =0x3f3f3f3f; typedefLong Longll;intf[n],d[n],t,n,a,b;CharCh[n];intFindsintx) {if(f[x]!=x) {intRoot =finds (f[x]); D[X]+=D[f[x]]; returnF[X] =Root; } returnx;}intMain () {scanf ("%d",&T); while(t--) {scanf ("%d",&N); for(intI=1; i<=n;i++) F[i] =i; memset (d,0,sizeof(d)); while(SCANF ("%s", ch) &&ch[0]!='O') { if(ch[0]=='E') {scanf ("%d",&a); intx =finds (a); printf ("%d\n", D[a]); } Else{scanf ("%d%d",&a,&b); F[a]=b; D[a]+ = (ABS (A-B)% +); } } } return 0;}
Uvalive 3027 corporative Network with right-check set