zoukankan      html  css  js  c++  java
  • SGU 149. Computer Network( 树形dp )

    题目大意:给N个点,求每个点的与其他点距离最大值 

    很经典的树形dp...很久前就想写来着...看了陈老师的code才会的...mx[x][0], mx[x][1]分别表示x点子树里最长的2个距离, dfs一遍得到. mx[x][2]表示从x的父亲到x的最长路径长度, 也是dfs一遍得到(具体看代码)。最后答案就是max(mx[x][0], mx[x][2]). 时间复杂度O(N)

    --------------------------------------------------------------------------------------------

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
     
    using namespace std;
     
    const int maxn = 10009;
     
    int N, mx[maxn][3];
     
    struct edge {
    int to, w;
    edge* next;
    } E[maxn << 1], *pt = E, *head[maxn];
     
    void AddEdge(int u, int v, int w) {
    pt->to = v; pt->w = w; pt->next = head[u]; head[u] = pt++;
    }
     
    void Init() {
    scanf("%d", &N);
    for(int i = 1; i < N; i++) {
    int t, w; scanf("%d%d", &t, &w); t--;
    AddEdge(i, t, w);
    AddEdge(t, i, w);
    }
    }
     
    inline void upd(int x, int t) {
    if(mx[x][1] < t)
    mx[x][1] = t;
    if(mx[x][0] < mx[x][1])
    swap(mx[x][0], mx[x][1]);
    }
     
    void DFS0(int x, int fa = -1) {
    mx[x][0] = mx[x][1] = 0;
    for(edge* e = head[x]; e; e = e->next) if(e->to != fa) {
    DFS0(e->to, x);
    upd(x, mx[e->to][0] + e->w);
    }
    }
     
    void DFS1(int x, int fa = - 1) {
    for(edge* e = head[x]; e; e = e->next) if(e->to != fa) {
    mx[e->to][2] = mx[x][2] + e->w;
    if(mx[e->to][0] + e->w == mx[x][0])
    mx[e->to][2] = max(mx[e->to][2], mx[x][1] + e->w);
    else
    mx[e->to][2] = max(mx[e->to][2], mx[x][0] + e->w);
    DFS1(e->to, x);
    }
    }
     
    int main() {
    Init();
    DFS0(0);
    DFS1(mx[0][2] = 0);
    for(int i = 0; i < N; i++)
    printf("%d ", max(mx[i][2], mx[i][0]));
    return 0;
    }

    --------------------------------------------------------------------------------------------

    149. Computer Network

    time limit per test: 0.25 sec.
    memory limit per test: 4096 KB
    input: standard input
    output: standard output



    A school bought the first computer some time ago. During the recent years the school bought N-1 new computers. Each new computer was connected to one of settled earlier. Managers of school are anxious about slow functioning of the net and want to know for each computer number Si - maximum distance, for which i-th computer needs to send signal (i.e. length of cable to the most distant computer). You need to provide this information.

    Input
    There is natural number N (N<=10000) in the first line of input, followed by (N-1) lines with descriptions of computers. i-th line contains two natural numbers - number of computer, to which i-th computer is connected and length of cable used for connection. Total length of cable does not exceed 10^9. Numbers in lines of input are separated by a space.

    Output
    Write N lines in output file. i-th line must contain number Si for i-th computer (1<=i<=N).

    Sample test(s)

    Input

    1 1 
    1 2

    Output


    3

    Author:Andrew V. Lazarev, Michael R. Mirzayanov
    Resource:Saratov Subregional School Team Contest, 2002
    Date:Fall, 2002







  • 相关阅读:
    vi编辑器更新...
    centos7 修改密码
    Linux的vi编辑器笔记
    linux 基本命令2(12月27日笔记)
    linux 进阶命令笔记(12月26日)
    linux 基础命令(12月25日笔记)
    谈谈CSS3中display属性的Flex布局(弹性布局)
    微信小程序页面跳转的三种方式总结
    微信小程序animation有趣的自定义动画
    微信小程序wx.setStorage(OBJECT)
  • 原文地址:https://www.cnblogs.com/JSZX11556/p/5043533.html
Copyright © 2011-2022 走看看