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;
const int maxn=;
struct node{int to,next,len;}edge[maxn<<];
int n,x,y,cnt,head[maxn],dp[maxn][],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){
int Dmax=,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){
if(v==lgst[u])dp[v][]=max(dp[u][],dp[u][])+edge[i].len;
else dp[v][]=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));
for(int i=;i<=n;++i){
scanf("%d%d",&x,&y);
add_edge(i,x,y);
add_edge(x,i,y);
}
dfs1(,-);
dfs2(,-);
for(int i=;i<=n;++i)
printf("%d\n",max(dp[i][],dp[i][]));
}
return ;
}

题解报告: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. IP数据报首部格式

    IP协议提供不可靠.无连接的数据报传送服务. 不可靠:尽力而为地传输,不保证IP数据报能成功到达目的地. 无连接:每一个数据报之间相互独立地进行路由选择,可不按发送顺序接收. IP首部格式例如以下: ...

  2. JavaScript重点记忆

    String的常用方法 indexOf() 返回字符串中检索指定字符第一次出现的位置 lastIndexOf() 返回字符串中检索指定字符最后一次出现的位置 match() 找到一个或多个正则表达式的 ...

  3. Java多态性详解 (父类引用子类对象)

    面向对象编程有三个特征,即封装.继承和多态. 封装隐藏了类的内部实现机制,从而可以在不影响使用者的前提下改变类的内部结构,同时保护了数据. 继承是为了重用父类代码,同时为实现多态性作准备.那么什么是多 ...

  4. android支付

    这里不讲具体的某个平台的支付使用,在工作中,公司使用到了ping++支付,使用它的好处是可以不用关心某个平台的支付了,例如:微信支付.支付宝支付等,太多的平台有个整合,是一个很好的事情,当然这也减轻了 ...

  5. 图解 servlet 与jsp的关系

    Servlet是Java提供的用于开发Web服务器应用程序的一个组件,运行在服务器端,由Servlet容器所管理,用于生成动态的内容.Servlet是平台独立的Java类,编写一个Servlet,实际 ...

  6. Android 单击跳转到打电话发短信界面,长按打电话发短信

    <uses-permission android:name="android.permission.CALL_PHONE"/><uses-permission a ...

  7. java包和javax包的区别

    基本类库和扩展类库 一般的lang,util都放在java.包 servlet放在javax包 以前sun把java中的叫核心库,把javax中的叫扩展库.现在sun已经把java和javax中的都叫 ...

  8. 学习html5 中的canvas(一)

    1.canvas画直线 <!doctype html> <html> <head> <meta charset="UTF-8"> & ...

  9. RK3288 GPIO 输出问题【转】

    本文转载自:http://m.blog.csdn.net/jiangdou88/article/details/50158673 #define GPIO_BANK0              (0 ...

  10. UVA11270 Tiling Dominoes —— 插头DP

    题目链接:https://vjudge.net/problem/UVA-11270 题意: 用2*1的骨牌填满n*m大小的棋盘,问有多少种放置方式. 题解: 骨牌类的插头DP. 1.由于只需要记录轮廓 ...