LA 3027 corporative Network (and check to find the distance from a node to the root node)

Source: Internet
Author: User

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 JI?? Jj (mod1000). 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 is 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

Main topic:

The title is that for a given n number of sets, I have 2 operations each time, one is I u V is to say, the ancestor of U is set to V (f[u]=v), then the distance from U to v becomes |u-v|%1000.

E U is said to ask you the distance to its ancestors.

Problem Solving Ideas:

Actually see the first operation I U V is relatively easy to get started, direct f[u] = V, on the line, but, the question is how to solve the problem efficiently dis[u] (U to its ancestors distance), is a more interesting question,

At first did not think out, saw the puzzle, found that he is then the path compression to the time, side modification, side maintenance of this array, until this u find his current ancestors. Drawing a picture is actually easy to see.

Code:

# include<cstdio># include<iostream># include<cmath>using namespace std;# define MAX 20004int F[MAX],        D[max];int n;void Init () {for (int i = 1;i <= n;i++) {F[i] = i;    D[i] = 0;    }}int getf (int x) {if (f[x]==x) return x;        else {int t = GETF (f[x]);        D[X]+=D[F[X]];        F[X] = t;    return f[x];    }}int Main (void) {int t; scanf ("%d", &t);        while (t--) {scanf ("%d", &n);        Init ();        String str;            while (CIN&GT;&GT;STR) {if (str[0]== ' O ') break;                if (str[0]== ' E ') {int T1; scanf ("%d", &t1);                int t2 = GETF (t1);            printf ("%d\n", D[t1]);                } else {int t1,t2; scanf ("%d%d", &t1,&t2);                F[T1] = T2;                int t3 = ABS (T1-T2);            D[T1] = t3%1000; }}} return 0;}

LA 3027 corporative Network (and check to find the distance from a node to the root node)

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.