zoukankan      html  css  js  c++  java
  • 【35.86%】【POJ 1962】Corporative Network

    Time Limit: 3000MS Memory Limit: 30000K
    Total Submissions: 3943 Accepted: 1414
    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 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 |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 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

    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
    Source

    Southeastern Europe 2004

    【题解】

    难点真的是这道题的翻译。。
    它的意思是说I代表合并操作。
    比如I X Y
    表示把X连同它的子树接在Y的下面,Y成为X的爸爸。
    同时X节点以及和它相连的节点的中心站点(根节点)就都变成了Y节点所在子树的中心站点(根节点)
    比如
    I 2 3
    E 2 ->输出为2到3的距离
    I 4 5
    E 4 ->输出为4到5的距离
    I 3 4
    E 2 ->输出为2到5的距离
    E 3 ->输出为3到5的距离
    然后就是用带权并查集来做了。
    挺简单的。
    就是在转移的时候注意。只有一开始的距离要取模。其他情况都不要取。不然会WA
    之前写过带权并查集的通解,配合相应的题看图解吧。
    http://blog.csdn.net/harlow_cheng/article/details/52737486

    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    const int MAXN = 29999;
    const int MOD = 1000;
    
    int f[MAXN], re[MAXN],n;
    
    void input(int &r)
    {
        r = 0;
        char t = getchar();
        while (!isdigit(t)) t = getchar();
        while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
    }
    
    int ff(int x)
    {
        if (f[x] == x)
            return x;
        int olfa = f[x];
        f[x] = ff(f[x]);
        re[x] = re[x] + re[olfa];//不能取模!
        return f[x];
    }
    
    int main()
    {
        //freopen("F:\rush.txt", "r", stdin);
        int T;
        input(T);
        while (T--)
        {
            input(n);
            for (int i = 1; i <= n; i++)
                f[i] = i, re[i] = 0;
            char key[4];
            scanf("%s", key);
            while (key[0] != 'O')
            {
                if (key[0] == 'I')
                {
                    int x, y;
                    input(x); input(y);
                    int a = ff(x), b = ff(y);
                    if (a != b)
                    {
                        f[a] = b;
                        re[a] = ((abs(x - y)) % MOD) + re[y] - re[x]; //外面别再取模了。
                    }
                }
                else
                {
                    int x;
                    input(x);
                    ff(x);
                    printf("%d
    ", re[x]);
                }
                scanf("%s", key);
            }
        }
        return 0;
    }
  • 相关阅读:
    Vue中axios基础使用(一)_前端前端请求数据
    vue中使用font-awesome
    vue-cli 搭建项目中,img引用资源404
    前端工程化常用的基础lunix命令
    vue运行项目时network显示unavailable
    关于vue中node_modules中第三方模块的修改使用
    tableau extension 调研
    使用 certbot 自动给 nginx 加上 https
    前端常用:复制到剪切板和下载
    ssh 的一个坑
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7632175.html
Copyright © 2011-2022 走看看