zoukankan      html  css  js  c++  java
  • BZOJ 1131 [POI2008] STA-Station 题解

    题目

    The first stage of train system reform (that has been described in the problem Railways of the third stage of 14th Polish OI.

    However, one needs not be familiar with that problem in order to solve this task.) has come to an end in Byteotia. The system consists of bidirectional segments of tracks that connect railway stations. No two stations are (directly) connected by more than one segment of tracks.

    Furthermore, it is known that every railway station is reachable from every other station by a unique route. This route may consist of several segments of tracks, but it never leads through one station more than once.

    The second stage of the reform aims at developing train connections.

    Byteasar count on your aid in this task. To make things easier, Byteasar has decided that:

    one of the stations is to became a giant hub and receive the glorious name of Bitwise, for every other station a connection to Bitwise and back is to be set up, each train will travel between Bitwise and its other destination back and forth along the only possible route, stopping at each intermediate station.

    It remains yet to decide which station should become Bitwise. It has been decided that the average cost of travel between two different stations should be minimal.

    In Byteotia there are only one-way-one-use tickets at the modest price of bythaler, authorising the owner to travel along exactly one segment of tracks, no matter how long it is.

    Thus the cost of travel between any two stations is simply the minimum number of tracks segments one has to ride along to get from one stations to the other.

    Task Write a programme that:

    reads the description of the train system of Byteotia, determines the station that should become Bitwise, writes out the result to the standard output.

    给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大

    输入格式

    给出一个数字(N),代表有(N)个点.(N<=1000000) 下面(N-1)条边.

    输出格式

    输出你所找到的点,如果具有多个解,请输出编号最小的那个.

    题解

    随便取一个点做根,比如1号节点,然后从1号节点出发dfs每个节点,算出每棵子树的大小和每个节点的深度

    然后再一次dfs,树形dp,求每个点做根时,所有点的深度之和,然后输出最大值即可.

    那么转移方程怎么考虑?

    这棵树从(fa)搜到(root)的时候,如何转移?

    dp值的含义是所有点的深度,那么在树根从(fa)变成(root)时,所有点的深度之和怎么变化?

    显然红色圈内所有点的深度+1,紫色圈内所有点深度-1

    紫色圈内点数就是以(root)为根的子树的大小,记为(size),则紫色圈内点数就是总点数减去(size)(n-size)

    所以转移方程就是:

    (dp_{root} = dp_{fa} - size_{root} + (n-size_{root}) \ = dp_{fa} + n - 2 imes size_{root} )

    还要注意用long long

    代码

    #include <cstdio>
    const int maxn = 1000005;
    int head[maxn], tot, n, ans, fa[maxn], size[maxn], ix, iy;
    long long dp[maxn];
    struct Edge { int to, next; } edges[maxn << 1];
    inline int input() { int t; scanf("%d", &t); return t; }
    void add(int x, int y) { edges[++tot].to = y; edges[tot].next = head[x]; head[x] = tot; }
    void dfs(int root, int fa) {
        size[root] = 1;
        for (int x = head[root]; x; x = edges[x].next) {
            if (edges[x].to == fa) continue;
            dfs(edges[x].to, root);
            size[root] += size[edges[x].to];
            dp[root] += dp[edges[x].to] + size[edges[x].to];
        }
    }
    void dpf(int root, int fa) {
        if (root != 1) dp[root] = dp[fa] + n - size[root] * 2;
        for (int x = head[root]; x; x = edges[x].next)
            if (edges[x].to != fa) dpf(edges[x].to, root);
    }
    int main() {
        n = input();
        for (int i = 1; i < n; i++) add(ix = input(), iy = input()), add(iy, ix);
        dfs(1, 0), dpf(1, 0);
        for (int i = 1; i <= n; i++) if (dp[i] > dp[ans]) ans = i;
        printf("%d
    ", ans);
    }
    
  • 相关阅读:
    javascript 中数字计算精度缺失问题
    javascript闭包
    MySQL数据库的创建
    原生项目使用 sass
    git工具命令
    如何将你的node服务放到线上服务器
    Cookie、Session、Token 的区别
    东北师大-构建之法-2020秋最终成绩(并非期末成绩)
    20201220-东北师范大学-助教-周总结-第14次
    东北师范大学-构建之法-20201207作业成绩
  • 原文地址:https://www.cnblogs.com/youxam/p/bzoj-1131.html
Copyright © 2011-2022 走看看