(DS "Algorithmic Competition Primer Classic") LA 3027 corporative Network (querying the distance between a node and the root node)

Source: Internet
Author: User

Main topic:

Querying the distance between a node and the root node


Problem Solving Ideas:

Weighted and check set problem. The previous topic was "to see if two or more nodes are under the same set", and now the topic is "Query a node to

The distance between the root nodes. Previously only need to use the father[x] this array, used to indicate who the Father node of X is. DIST[X] Array is now introduced to record

The distance from the X node to the root node

1) in and check the concentration, the root node does not understand, the other nodes can move.



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 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, 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 are able in each moment to answer the questions of the users.

InputYour program have to is ready -to-solve more than one test case. The first line of the input file 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 its 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.

OutputThe output should contain as many lines as the number of E commands in all test cases with a single number each ? Asked 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

The code is as follows:

/* * LA3027.cpp * *  Created on:2015 January 3 *      author:administrator * * #include <iostream> #include <cstdio& gt; #include <algorithm>using namespace std;const int maxn = 20005;int father[maxn];int dist[maxn];int Find (int x) {i F (x! = Father[x]) {int FX = find (father[x]);d ist[x] + = dist[father[x]];father[x] = FX;} return father[x];}  int main () {int t;scanf ("%d", &t), while (t--) {char cmd[9];int n;scanf ("%d", &n); int i;for (i = 1; I <= n; ++i) {Father[i] = i;dist[i] = 0;} while (scanf ("%s", cmd) = = 1, cmd[0]! = ' O ') {if (cmd[0] = = ' I ') {int u, v;scanf ("%d%d", &u, &v); Father[u] = V;dis T[u] = ABS (U-V)% 1000;} else if (cmd[0] = = ' E ') {int u;scanf ("%d", &u), Find (U);p rintf ("%d\n", Dist[u]);}} return 0;}




(DS "Algorithmic Competition Primer Classic") LA 3027 corporative Network (querying the distance between a node and 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.