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 tele Communication Center. Soon, for Amelioration of the services, the corporation started to collect some enterprises in clusters, each of them serv Ed 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 CLU Ster 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, the clusters is joined in a new cluster, and served by the center of the old cluster B. Unfortunately a fter each join the sum of the lengths of the lines linking a enterprise to their serving center could be changed and the En D 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.
Input
Your program have to is ready-to-solve more than one test case. The first line of the input 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 is finishes 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 Aske D sum of length of lines connecting the corresponding enterprise with its serving center.
Sample Input
14E 3I 3 1E 3I 1 2E 3I 2 4E 3O
Sample Output
0235
"Test Instructions" gives the example of T, there are N nodes, just start the node with its own end, and then I give two X, Y, after I gave an X to point to the y,e, and asked the end of X and his distance mod1000;
"Solution" and check the set
#include <iostream>#include<math.h>#include<algorithm>#include<stdio.h>#include<string.h>using namespacestd;Const intinf=0x3f3f3f3f;Const intn=20005;intt,n,x,y;Charstr[5];intFa[n],dis[n];intFindx (intx) { if(x==Fa[x])returnx; intfx=findx (fa[x]); DIS[X]+=Dis[fa[x]]; returnfa[x]=FX;}voidMergeintXinty) { intFx=findx (x), fy=findx (y); if(fx==FY) { return ; } Fa[x]=x;dis[fx]=Dis[x]; FA[X]=y; DIS[X]=abs (x-y)% +;}voidinit () { for(intI=0; i<=n;i++) {Dis[i]=0; Fa[i]=i; }}intMain () {scanf ("%d",&t); while(t--) {scanf ("%d",&N); Init (); while(~SCANF ("%s", str)) { if(str[0]=='O') Break; Else if(str[0]=='E') {scanf ("%d",&x); FINDX (x); printf ("%d\n", dis[x]); } Else{scanf ("%d%d",&x,&y); Merge (x, y); } } } return 0;}
Corporative Network_ and check set