"Summer Vacation" [Practical data structure]uvalive 3027 corporative Network

Source: Internet
Author: User

Uvalive 3027 corporative Network

Topic:

corporative Network
Time Limit: 3000MS Memory Limit: 30000K
Total Submissions: 3450 Accepted: 1259

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


--------------------------------------------------------------------------------------------------------------- --------------------------------------------

Ideas:
The operation can be implemented using the and check set only if the root distance is queried and the parent node is changed . Add information for each node D represents the distance to the parent node, for the I operation, D is initialized to ABS (U-V)%1000, and for each query e add function: Compress path while changing D, that is, D[u]=dist (u->root) at P[u]=root

Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4 #definefor (A,B,C) for (int a= (b);a< (c); a++)5 using namespacestd;6 Const intmaxn=20000+Ten;7 8 intP[MAXN],D[MAXN];9 intFind_set (intu) {Ten //Find root node--path compression + maintenance D One     if(U==p[u])returnu; A     Else -      { -          introot=Find_set (P[u]); theD[u] + = D[p[u]];//Update D for d->root distance -          returnP[u]=root;//Path Compression -      } - } +  - intMain () { + intT,n; scanf"%d",&T); A    while(t--){ atscanf"%d",&n); -for (I,1, n+1) {p[i]=i; d[i]=0; }  -       Charch[5];intx, y; -        while(SCANF ("%s", ch) && ch[0]!='O'){ -         if(ch[0]=='E') {scanf ("%d", &x); Find_set (x);p rintf ("%d\n", d[x]); } -       Else{scanf ("%d%d", &x,&y); P[x]=y; D[x]=abs (x-y)% +; } in       } -   } to   return 0; +}

"Summer Vacation" [Practical data structure]uvalive 3027 corporative Network

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.