Corporative Network

来源:互联网 发布:免费注册淘宝会员 编辑:程序博客网 时间:2024/03/28 17:12

 

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
Submit Status

Description

Download as PDF

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 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 cluster B (not necessarily the center) and link them with telecommunication line. The length of the line between the enterprises I and J is |I – J|(mod 1000). In such a way the two old clusters are 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 is able in each moment to answer the questions of the users.

Input 

Your program has to be ready to solve more than one test case. The first line of the input file will contains only the number T of the test cases. Each test will start with the number N of enterprises (5≤N≤20000). Then some number of lines (no more than 200000) will 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 is linked to the enterprise J.
The test case finishes with a line containing the word O. The I commands are 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 

14E 3I 3 1E 3I 1 2E 3I 2 4E 3O

Sample Output 

0235

题意:A 和B 两个群, 从A 中选取一主机, 连接B 中一机, 不一定要主机, 累计两机间的距离..连接后两机变成一新群...主机为B 群机...

        I  I  J   连接两机

        E I   问此机到主机的距离

思路:  这道题折腾了两天,  结果是一开始没有读懂题意, 主机弄错了~

         其实就是简单的并查集...

         若数据为3 2,2 1,1 4, 5 6,6 7  ;E 1

         3->2->1->4

                      |

         5->6->7             最后主机是 7

code:

#include<stdio.h>#include<iostream>#include<string.h>#include<math.h>using namespace std;int rel[20005];int set[20005];void init(int nn){    for(int i=1;i<=nn;i++)    {        set[i]=i;        rel[i]=0;    }}int find(int x){    int t;    if(x==set[x])return x;    else t=find(set[x]);    rel[x]+=rel[set[x]];    return set[x]=t;}void unite(int x,int y){    if(x==y)return ;    set[x]=y;    rel[x]=((int)fabs(x-y))%1000;}int main(){    //freopen("B.txt","r",stdin);    int T,n;    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        getchar();        init(n);        int a,b;        char ei;        scanf("%c",&ei);        while(ei!='O')        {            if(ei=='E')            {                scanf("%d",&a);                find(a);                printf("%d\n",rel[a]);            }            if(ei=='I')            {                scanf("%d%d",&a,&b);                getchar();                unite(a,b);            }            scanf("%c",&ei);        }    }    return 0;}


0 0