zoukankan      html  css  js  c++  java
  • Computer(树上最远距离 树形DP)

    Computer

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


    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
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 int n;
     5 const int maxn=1e4+5;
     6 struct Edge{
     7     int v,w,next;
     8 }edge[maxn<<1];
     9 int f[maxn],g[maxn],fa[maxn];  //f第一步往下走的走远距离,g第一步往往上走的组就远距离
    10 
    11 int head[maxn],cnt;
    12 void init(){ memset(head,-1,sizeof(head)),cnt=0; for(int i=1;i<maxn;i++) f[i]=g[i]=fa[i]=0; }
    13 void add(int u,int v,int w){
    14     edge[cnt].v=v;
    15     edge[cnt].w=w;
    16     edge[cnt].next=head[u];
    17     head[u]=cnt++;
    18 }
    19 
    20 int dfs(int u,int pre){   ///自底往上
    21     fa[u]=pre;
    22     for(int i=head[u];~i;i=edge[i].next){
    23         int x=edge[i].v;
    24         if(x!=pre){
    25             f[u]=max(f[u],edge[i].w+dfs(x,u));
    26         }
    27     }
    28     return f[u];
    29 }
    30 
    31 void dfs2(int u,int pre){   ///自上往下
    32     g[u]=g[pre];
    33     int t=0;
    34     for(int i=head[pre];~i;i=edge[i].next){
    35         int v=edge[i].v,w=edge[i].w;
    36         if(v==fa[pre]) continue;  //祖先的祖先
    37         if(v==u) t=w;
    38         else g[u]=max(g[u],f[v]+w);
    39     }
    40 //    if(pre!=0)
    41         g[u]+=t;
    42     for(int i=head[u];~i;i=edge[i].next){
    43         if(edge[i].v!=pre) dfs2(edge[i].v,u);
    44     }
    45 }
    46 
    47 int main(){
    48     while(~scanf("%d",&n)){
    49         init();
    50         for(int i=2,d1,d2;i<=n;i++){
    51             scanf("%d%d",&d1,&d2);
    52             add(i,d1,d2);
    53             add(d1,i,d2);
    54         }
    55         dfs(1,0);
    56         dfs2(1,0);
    57 
    58         for(int i=1;i<=n;i++){
    59             printf("%d
    ",max(f[i],g[i]));
    60         }
    61     }
    62     return 0;
    63 }
    View Code
     
     
  • 相关阅读:
    CentOS安装sctp协议
    视频编码未来简史
    Linux内核:分析coredump文件
    skb的两个函数pskb_copy和skb_copy
    《Linux内核设计与实现》读书笔记(十二)- 内存管理
    Linux内核学习笔记之seq_file接口创建可读写proc文件
    内核如何签名
    《女士品茶》与统计检验
    K近邻算法
    PCA原理分析
  • 原文地址:https://www.cnblogs.com/qq-1585047819/p/11941050.html
Copyright © 2011-2022 走看看