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
解题思路:求出树中每个节点能达到的最远距离。对于每个节点u来说,其可能到达的最长距离为max{其子树内的最长距离,其父节点不经过u的子树内的最长距离}。结合树的直径求法(1次dfs)便可得到最长链、每个节点的最长距离dp[u][0]和次长距离dp[u][1]。再一次dfs就是求节点u的反向最长距离即其父节点不经过u的子树内的最长距离dp[u][2],有两种情况:①如果当前节点v不在树的直径上,那么dp[v][2]的值为max{其父节点u所在子树的最长距离dp[u][0],其父节点u的父节点不经过u的子树内的最长距离即u的反向距离dp[u][2]}+dist(u,v);例如5号节点,其dp[5][2]=max{dp[2][0],dp[2][2]}+1(2,5)=[5--2--1--6--7]=3+1=4。②如果当前节点v在树的直径上,那么dp[v][2]的值为max{其父节点u的反向最长距离dp[u][2],其父节点的次长距离dp[u][1]}+dist(u,v),例如3号节点,其dp[3][2]=max{dp[2][1],dp[2][2]}+1(2,3)=[3--2--1--6--7]=3+1=4。最后每个节点u能达到的最远距离就是max{正向最长距离dp[u][0],反向最长距离dp[u][2]}。
AC代码(31ms):
 #include<bits/stdc++.h>
 using namespace std;
 ;
 ];
 ],lgst[maxn];
 void add_edge(int u,int v,int w){
     edge[cnt].to=v;
     edge[cnt].len=w;
     edge[cnt].next=head[u];
     head[u]=cnt++;
 }
 int dfs1(int u,int fa){
     ,Dsec=;
     for(int i=head[u];~i;i=edge[i].next){
         int v=edge[i].to;
         if(v^fa){
             int nowd=dfs1(v,u)+edge[i].len;
             if(nowd>Dmax)lgst[u]=v,Dsec=Dmax,Dmax=nowd;//lgst[u]=v记录u的正向最长路径上的节点v
             else if(nowd>Dsec)Dsec=nowd;
         }
     }
     dp[u][]=Dmax,dp[u][]=Dsec;//记录每个节点的正向最长距离和正向次长距离
     return Dmax;//返回正向最长距离
 }
 void dfs2(int u,int fa){
     for(int i=head[u];~i;i=edge[i].next){
         int v=edge[i].to;
         if(v^fa){
             ]=max(dp[u][],dp[u][])+edge[i].len;
             ]=max(dp[u][],dp[u][])+edge[i].len;
             dfs2(v,u);
         }
     }
 }
 int main(){
     while(~scanf("%d",&n)){
         cnt=;memset(head,-,sizeof(head));
         memset(dp,,sizeof(dp));
         memset(lgst,,sizeof(lgst));
         ;i<=n;++i){
             scanf("%d%d",&x,&y);
             add_edge(i,x,y);
             add_edge(x,i,y);
         }
         dfs1(,-);
         dfs2(,-);
         ;i<=n;++i)
             printf(],dp[i][]));
     }
     ;
 }

题解报告:hdu 2196 Computer(树形dp)的更多相关文章

  1. HDU 2196.Computer 树形dp 树的直径

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  2. HDU 2196 Computer 树形DP经典题

    链接:http://acm.hdu.edu.cn/showproblem.php? pid=2196 题意:每一个电脑都用线连接到了还有一台电脑,连接用的线有一定的长度,最后把全部电脑连成了一棵树,问 ...

  3. HDU 2196 Computer 树形DP 经典题

    给出一棵树,边有权值,求出离每一个节点最远的点的距离 树形DP,经典题 本来这道题是无根树,可以随意选择root, 但是根据输入数据的方式,选择root=1明显可以方便很多. 我们先把边权转化为点权, ...

  4. hdu 2196 Computer(树形DP)

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  5. hdu 2196 Computer 树形dp模板题

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  6. hdu 2196 Computer(树形DP经典)

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  7. HDU 2196 Computer (树dp)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2196 给你n个点,n-1条边,然后给你每条边的权值.输出每个点能对应其他点的最远距离是多少 ...

  8. HDU - 2196(树形DP)

    题目: A school bought the first computer some time ago(so this computer's id is 1). During the recent ...

  9. hdu 2196【树形dp】

    http://acm.hdu.edu.cn/showproblem.php?pid=2196 题意:找出树中每个节点到其它点的最远距离. 题解: 首先这是一棵树,对于节点v来说,它到达其它点的最远距离 ...

  10. HDU 2196 Compute --树形dp

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

随机推荐

  1. 9.5---括号是否有效(CC150)

    leetcode原题: char temp ; Stack<Character> stack = new Stack<Character>();//error:Stack< ...

  2. memcache 内部原理实现

    Lazy Expiration memcached 内部不会监视记录是否过期,而是在 get 时查看记录的时间戳,检查记录是否过期.这 种技术被称为 lazy(惰性)expiration.因此,mem ...

  3. QTP10&amp;QTP11&amp;UFT11.5的安装和破解

    QTP10的安装和破解方法 下载QTP10.0并安装. 安装成功后,在C:\Program Files\Common Files\Mercury Interactive下创建文件夹:License M ...

  4. c程序设计语言_习题1-16_自己编写getline()函数,接收整行字符串,并完整输出

    Revise the main routine of the longest-line program so it will correctly print the length of arbitra ...

  5. (转) Special members

    原地址:http://www.cplusplus.com/doc/tutorial/classes2/   Special members [NOTE: This chapter requires p ...

  6. MCMC,GIBBS SAMPLING简单摘要

    本文后面很多内容都是参考博客:http://www.cnblogs.com/xbinworld/p/4266146.html.本文主要用作学习交流备忘用. 1)简述: 随机模拟也可以叫做蒙特卡洛模拟, ...

  7. Erlang edoc 多级目录出错

    Erlang edoc 多级目录出错使用rebar doc来生成项目文档.但是当erl源文件目录src下建立子目录,并新建erlang文件后,就无法生成文档. 例如,新建 src/tttt/, 并添加 ...

  8. JS前端无侵入实现防止重复提交请求技术

    JS前端无侵入实现防止重复提交请求技术 最近在代码发布测试的过程中,我发现有些请求非常的消耗服务器资源,而系统测试人员因为响应太慢而不停的点击请求.我是很看不惯系统存在不顺眼的问题,做事喜欢精益求精, ...

  9. smarty学习——内建函数(部分接上)

    9.{foreach} {foreachelse} 格式如下: {foreach $arrayvar as $itemvar} {foreach $arrayvar as $keyvar=>$i ...

  10. Fiddler设置代理后,iphone手机无法访问网络的全面解决办法

    好久不抓手机包,都有些忘记了,电脑上软件都在,原本是Fiddler4,现在已自动升级到V5. 入主题吧,首先Fiddler设置好代理,然后手机安装证书好了,设置手机代理,结果iphone无法访问网络, ...