zoukankan      html  css  js  c++  java
  • hdu2196 Computer 树形dp

    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.

    InputInput 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.OutputFor 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

    题意:
        就是让你求一颗树上每个点能到达的最远距离。
    题解:
        一开始看到数据范围10000怎么回事,(⊙o⊙)…,这么小,不对啊。
        结果后来发现多组数据。
        其实很简单,一次dfs处理处最长和次长路。
        然后dfs从祖先转移一个g数组表示从祖先出去可以到达的最远距离。
     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cmath>
     6 #define N 10007
     7 using namespace std;
     8 
     9 int n;
    10 int ans[N];
    11 int cnt,head[N],Next[N*2],rea[N*2],val[N*2],g[N];
    12 struct Node
    13 {
    14     int dis,com;
    15 }f[N][4];
    16 
    17 void add(int u,int v,int fee)
    18 {
    19     Next[++cnt]=head[u];
    20     head[u]=cnt;
    21     rea[cnt]=v;
    22     val[cnt]=fee;
    23 }
    24 void dfs_son(int u,int fa)
    25 {
    26     for (int i=head[u];i!=-1;i=Next[i])
    27     {
    28         int v=rea[i],fee=val[i];
    29         if (v==fa) continue;
    30         dfs_son(v,u);
    31         if (f[v][1].dis+fee>f[u][1].dis)
    32         {
    33             f[u][2]=f[u][1];
    34             f[u][1].dis=f[v][1].dis+fee;
    35             f[u][1].com=v;
    36         }
    37         else if (f[v][1].dis+fee>f[u][2].dis)
    38              {
    39                   f[u][2].dis=f[v][1].dis+fee;
    40                   f[u][2].com=v;
    41              }
    42     }
    43     ans[u]=max(ans[u],f[u][1].dis);
    44 }
    45 void dfs_fa(int u,int fa)
    46 {
    47     for (int i=head[u];i!=-1;i=Next[i])
    48     {
    49         int v=rea[i],fee=val[i];
    50         if (v==fa) continue;
    51         if (f[u][1].com!=v) g[v]=max(g[u]+fee,fee+f[u][1].dis);
    52         else g[v]=max(g[u]+fee,fee+f[u][2].dis);
    53         ans[v]=max(ans[v],g[v]);
    54     }
    55     for (int i=head[u];i!=-1;i=Next[i])
    56     {
    57         int v=rea[i];
    58         if (v==fa) continue;
    59         dfs_fa(v,u);
    60     }
    61 }
    62 int main()
    63 {
    64     while (~scanf("%d",&n))
    65     {
    66         cnt=0;
    67         memset(head,-1,sizeof(head));
    68         memset(f,0,sizeof(f));
    69         memset(g,0,sizeof(g));
    70         memset(ans,0,sizeof(ans));
    71         int x,y;
    72         for (int i=2;i<=n;i++)
    73         {
    74             scanf("%d%d",&x,&y);
    75             add(i,x,y),add(x,i,y);
    76         }
    77         dfs_son(1,-1);
    78         dfs_fa(1,-1);
    79         for (int i=1;i<=n;i++)
    80             printf("%d
    ",ans[i]);
    81     }
    82 }
     
  • 相关阅读:
    BFC是什么?如何形成BFC,有什么作用?
    z-index 有什么作用? 如何使用?
    有几种定位方式?分别是如何实现定位的?参考点是什么?使用场景是什么?
    Promise
    token
    文档碎片
    ECharts常用配置项
    for each()和map()的区别
    window.onload和$(docunment).ready的区别
    密码的两种常用加密方式
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/7735010.html
Copyright © 2011-2022 走看看