Poj1962:corporative Network (and collection)

Source: Internet
Author: User

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

Source

Southeastern Europe 2004
Test instructions: There are n points, starting at each point with itself as the signal endpoint, when input i X Y, connect x, Y, and the end of the signal to Y,e x is the output x distance from the signal end point
Idea: Classic and check set, just each node with right, need to update
#include <iostream> #include <stdio.h> #include <string.h> #include <stack> #include <queue > #include <map> #include <set> #include <vector> #include <math.h> #include <algorithm >using namespace std, #define LS 2*i#define rs 2*i+1#define up (i,x,y) for (i=x;i<=y;i++) #define DOWN (i,x,y) for (i=x; i>=y;i--) #define MEM (a,x) memset (A,x,sizeof (a)) #define W (a) while (a) #define LL long longconst double pi = acos (-1.0); # Define Len 20005#define mod 19999997const int INF = 0x3f3f3f3f;int T,n;char str[5];int father[len],dis[len];int Find (int x    {if (x = = Father[x]) return x;    int FX = FIND (father[x]);    DIS[X] = dis[x]+dis[father[x]]; return FATHER[X]=FX;}    void solve (int x,int y) {int fx = find (x), FY = find (y);    if (FX = = FY) return;    FATHER[X] = x;    DIS[FX] = dis[x];    Father[x] = y; DIS[X] = ABS (x-y)%1000;}    int main () {int i,j,k,x,y;    scanf ("%d", &t);        W (t--) {scanf ("%d", &n); Up (I,0,n)        {Father[i] = i;        Dis[i] = 0;            } w (~scanf ("%s", str)) {if (str[0]== ' O ') break;                if (str[0]== ' E ') {scanf ("%d", &x);                Find (x);            printf ("%d\n", dis[x]);                } else {scanf ("%d%d", &x,&y);            Solve (x, y); }}} return 0;}


Poj1962:corporative Network (and collection)

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.