zoukankan      html  css  js  c++  java
  • HDU2196(SummerTrainingDay13-D tree dp)

    Computer

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 30923    Accepted Submission(s): 3861


    Problem Description

    A school bought the first computer some time ago(so this computer's id is 1). 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 the maximum distance Si 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. 


    Hint: the example input is corresponding to this graph. And from the graph, you can see that the computer 4 is farthest one from 1, so S1 = 3. Computer 4 and 5 are the farthest ones from 2, so S2 = 2. Computer 5 is the farthest one from 3, so S3 = 3. we also get S4 = 4, S5 = 4.
     

    Input

    Input file contains multiple test cases.In each case there is natural number N (N<=10000) in the first line, 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

    For each case output N lines. i-th line must contain number Si for i-th computer (1<=i<=N).
     

     

    Sample Input

    5 1 1 2 1 3 1 1 1
     

     

    Sample Output

    3 2 3 4 4
     

    Author

    scnu
     
    题意:问从每个几点出发所到达的最远距离。
    思路:两遍dfs,一遍从上往下,一遍从下往上,答案为往上走或往下走的最大值。
     1 //2017-09-13
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <iostream>
     5 #include <algorithm>
     6 
     7 using namespace std;
     8 
     9 const int N = 11000;
    10 
    11 int head[N], tot;
    12 struct Edge{
    13     int v, w, next;
    14 }edge[N<<1];
    15 
    16 void init(){
    17     tot = 0;
    18     memset(head, -1, sizeof(head));
    19 }
    20 
    21 void add_edge(int u, int v, int w){
    22     edge[tot].v = v;
    23     edge[tot].w = w;
    24     edge[tot].next = head[u];
    25     head[u] = tot++;
    26 }
    27 
    28 //down[u][0]表示u节点往下走的最大距离,down[u][1]表示节点u往下走的次大距离
    29 //up[u]表示节点u往上走的最大距离,son[u]表示u节点往下走的最大距离对应的儿子
    30 int n, down[N][2], up[N], son[N];
    31 
    32 void dfs1(int u, int fa){
    33     for(int i = head[u]; i != -1; i = edge[i].next){
    34         int v = edge[i].v, w = edge[i].w;
    35         if(v == fa)continue;
    36         dfs1(v, u);
    37         if(down[v][0]+w > down[u][0]){//更新最大的情况
    38             down[u][1] = down[u][0];
    39             down[u][0] = down[v][0]+w;
    40             son[u] = v;
    41         }else if(down[v][0]+w > down[u][1])//只更新次大值的情况
    42               down[u][1] = down[v][0] + w;
    43     }
    44 }
    45 
    46 void dfs2(int u, int fa){
    47     for(int i = head[u]; i != -1; i = edge[i].next){
    48         int v = edge[i].v, w = edge[i].w;
    49         if(v == fa)continue;
    50         if(son[u] != v)
    51             up[v] = max(up[u]+w, down[u][0]+w);
    52         else
    53             up[v] = max(up[u]+w, down[u][1]+w);
    54         dfs2(v, u);
    55     }
    56 }
    57 
    58 int main()
    59 {
    60     //freopen("inputD.txt", "r", stdin);
    61     while(scanf("%d", &n) != EOF){
    62         init();
    63         int v, w;
    64         for(int i = 2; i <= n; i++){
    65             scanf("%d%d", &v, &w);
    66             add_edge(i, v, w);
    67             add_edge(v, i, w);
    68         }
    69         memset(up, 0, sizeof(up));
    70         memset(down, 0, sizeof(down));
    71         dfs1(1, 0);
    72         dfs2(1, 0);
    73         for(int i = 1; i <= n; i++)
    74               printf("%d
    ", max(up[i], down[i][0]));
    75     }
    76 
    77     return 0;
    78 }
  • 相关阅读:
    MySQL数据库详解(二)执行SQL更新时,其底层经历了哪些操作?
    MySQL数据库详解(一)执行SQL查询语句时,其底层到底经历了什么?
    网页静态化解决方案Freemarker
    好久没来看看了~
    springmvc(五) 数据回显与自定义异常处理器
    springmvc(四) springmvc的数据校验的实现
    springmvc(三) 参数绑定、
    springmvc(二) ssm框架整合的各种配置
    springmvc(一) springmvc框架原理分析和简单入门程序
    cursor:pointer 什么意思?
  • 原文地址:https://www.cnblogs.com/Penn000/p/7514929.html
Copyright © 2011-2022 走看看