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;
}
时间: 2024-10-28 21:58:24

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

[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

(DS 《算法竞赛入门经典》)LA 3027 Corporative Network(查询某一个节点到根节点之间的距离)

题目大意: 查询某一个节点到根节点之间的距离 解题思路: 加权并查集问题.之前做的题目是"查看两个或多个节点是否在同一个集合下",现在的题目是"查询某个节点到 根节点之间的距离".之前只需要使用到father[x]这个数组,用来表示x的父亲节点是谁.现在引入dist[x]数组,用来记录 x节点到根节点的距离 1)在并查集中,根节点不懂,其他节点都可以动. A very big corporation is developing its corporative net

POJ1962:Corporative Network(并查集)

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 telecommunication center. Soon, for amelioration of the se

【LA 3027 Corporative Network】

·一些很可爱的询问和修改,放松地去用并查集解决. ·英文题,述大意: 输入n(5<=n<=20000)表示树有n个节点,并且会EOF结束地读入不超过 20000个操作,一共有两种:    ①I v u:表示将v的父亲节点设置为u(在这之前v没有爸爸),边权设置为abs(v-u)%1000.    ②E u:表示询问u到当前它所在树的根节点的距离. ·分析:      为了记录当前一系列加边操作后所有的点的位置情况(因为你随时可能回答询问啊),根据这道题的点关系特点[只在乎点和其根节点的信息],

并查集(路径更新) LA 3027 Corporative Network

题目传送门 题意:训练指南P192 分析:主要就是一个在路径压缩的过程中,更新点i到根的距离 #include <bits/stdc++.h> using namespace std; const int N = 2e4 + 5; struct DSU { int rt[N], d[N]; void init(void) { memset (rt, -1, sizeof (rt)); memset (d, 0, sizeof (d)); } int Find(int x) { if (rt[x

LA 3027 Corporative Network(并查集)

有n个点,一开始都是孤立的,然后有I,E两种操作 I        u v,把u的父节点设为v,距离为abs(u-v) % 1000,保证u之前没有父节点 E      询问u到根节点的距离 #include<iostream> #include<cmath> using namespace std; const int maxn=2e4+5; int par[maxn],dis[maxn]; void init(int n) { for(int i=0;i<=n;i++)

LA 3027 Corporative Network

这题感觉和 POJ 1988 Cube Stacking 很像,在路径压缩的同时递归出来的时候跟新distant数组 1 //#define LOCAL 2 #include <algorithm> 3 #include <cstdio> 4 using namespace std; 5 6 const int maxn = 20000 + 10; 7 int parent[maxn], distant[maxn]; 8 9 int GetParent(int a) 10 { 11

UVALive - 3027 - Corporative Network (并查集!!)

UVALive - 3027 Corporative Network Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A very big corporation is developing its corporative network. In the beginning each of the N enterprises of the cor

UVA 3027 Corporative Network 带权并查集、

题意:一个企业要去收购一些公司把,使的每个企业之间互联,刚开始每个公司互相独立 给出n个公司,两种操作 E I:询问I到I它连接点最后一个公司的距离 I I J:将I公司指向J公司,也就是J公司是I公司的上级,距离为abs(I-J)%1000(貌似G++不支持abs,PE了两发) 思路:转化一下题意就行了,首先刚开始的时候每个公司都是独立的,I操作就是并查集中合并操作,将I这课树并到J这个树上, E操作要求的东西就是 I到I的根节点的距离,先看一个没有路径压缩直接暴力的方法把.(本以为不会过的,