LA 3027 Corporative Network(并查集,求某个节点到根节点的距离)

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 other
cluster B (not necessarily the center) and link them with telecommunication line. The length of the
line between the enterprises I and J is jI 􀀀Jj(mod1000). 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.
Your program has to be ready to solve more than one test case.
Input
The rst line of the input le 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 nishes 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
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

 

题目大意:

  题目是说, 对于一个给定n个数字的集合,我每次有2种操作,一个是I u v 是说,把u的祖先设置为v(f[u]=v),然后u到v的距离变成|u-v|%1000.

  E u 是说,询问u到其祖先的距离。

解题思路:

  其实看到第一个操作 I u v 还是比较容易上手的,直接f[u] = v ,就行了,但是,这个题的问题是怎么样高效的求解出来dis[u](u到其祖先的距离),是个比较有意思的问题,

一开始没有想出来,看到了题解后,发现他是再进行路径压缩到时候,边修改,边维护这个数组,直到这个u找到了他目前的祖先。画个图其实就很容易看出来了。

 

代码:

# include<cstdio>
# include<iostream>
# include<cmath>

using namespace std;

# define MAX 20004

int 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>>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;
}

 

posted @ 2015-08-19 17:51  BYYB_0506  阅读(575)  评论(0编辑  收藏  举报