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 }
  • 相关阅读:
    element-ui 多张图上传
    js json生取key,value 值
    iview DatePicker 回显验证报错
    iview的Select控制value为数字类型时表单验证无法通过
    iview 自定义树形
    tree 树形递归修改 key
    根据月份选择 生成这个月的每一天
    微信小程序超出隐藏省略号和自动换行
    uni-app picker select 取想要的值
    element-ui 表格fixed 样式修改
  • 原文地址:https://www.cnblogs.com/Penn000/p/7514929.html
Copyright © 2011-2022 走看看