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
题目大意:
给定n,代表n台电脑,编号为1的是最初始的电脑,下面n-1对数字,分别编号为2~n的电脑相连电脑的编号与长度。
输出n行,求与第i台电脑最远的电脑的距离。
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int n,dp[],pre[];
struct edge
{
int u,v,w,p;///u与v电脑相连,长度w,u对应的上一条边
edge(){}
edge(int u,int v,int w,int p):u(u),v(v),w(w),p(p){}
}e[];
int dfs(int u,int p)
{
int ans=;
for(int i=pre[u];i!=-;i=e[i].p)
{
int v=e[i].v;
if(v==p) continue;
if(!dp[i])///没更新过
dp[i]=dfs(v,u)+e[i].w;
ans=max(ans,dp[i]); }
return ans;
}
int main()
{
while(cin>>n)
{
memset(dp,,sizeof dp);
memset(pre,-,sizeof pre);
int x=;
for(int i=,v,w;i<=n;i++)
{
cin>>v>>w;
e[x]=edge(i,v,w,pre[i]);
pre[i]=x++;
e[x]=edge(v,i,w,pre[v]);
pre[v]=x++;
}
for(int i=;i<=n;i++)
cout<<dfs(i,-)<<'\n';
}
return ;
}
 

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. computer(树形dp || 树的直径)

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

  3. HDU 2196 Computer 树形DP 经典题

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

  4. HDU 2196 Computer 树形DP经典题

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

  5. hdu 2196 Computer(树形DP)

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

  6. hdu 2196(Computer 树形dp)

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

  7. hdu-2169 Computer(树形dp+树的直径)

    题目链接: Computer Time Limit: 1000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) ...

  8. 【HDU 2196】 Computer (树形DP)

    [HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...

  9. hdu 2196 Computer 树形dp模板题

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

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

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

随机推荐

  1. JAVA常用知识总结(四)——集合

    先附一张java集合框架图 下面根据面试中常问的关于集合的问题进行了梳理: Arraylist 与 LinkedList 有什么不同? 1. 是否保证线程安全: ArrayList 和 LinkedL ...

  2. 479 Largest Palindrome Product 最大回文数乘积

    你需要找到由两个 n 位数的乘积组成的最大回文数.由于结果会很大,你只需返回最大回文数 mod 1337得到的结果.示例:输入: 2输出: 987解释: 99 x 91 = 9009, 9009 % ...

  3. Centos 6.5安装MySQL-Python遇到的问题--解决办法一

    系统:CentOS release 6.5 (Final) MySQL版本:mysql  Ver 14.14 Distrib 5.7.19, for Linux (x86_64) using  Edi ...

  4. 堆参数-XMS 与-XMX的说明

    XMS : JVM初始分配的堆内存 XMX : JVM最大允许分配的堆内存,按需分配 堆内存分配: JVM初始分配的堆内存由-Xms指定,默认是物理内存的1/64: JVM最大分配的堆内存由-Xmx指 ...

  5. ASP.NET MVC Identity 兩個多個連接字符串問題解決一例

    按照ASP.NET MVC Identity建立了一個用戶權限管理模塊,由于還要加自己已有的數據庫,所以建立了一個實體模型,建立了之后,發現登錄不了: 一直顯示“Login in failed for ...

  6. npm安装使用及vue脚手架安装

    公司在前端用vue开发项目,那就学习下啦,第一步,在安装vue-devtools过程中,npm作为官方manual installtion方式,肯定必不可少. NPM是随同NodeJS一起安装的包管理 ...

  7. Windows虚拟桌面

    PROCESS_INFORMATION ProcessInfo; STARTUPINFO StartupInfo; HDESK hDesktop; HDESK hOriginalThread; HDE ...

  8. Python 面向对象 特殊方法(魔法方法)

    Python 的特殊方法,两边带双下划线的方法. 比如:__init__(self, ...).__del__(self) 1.__init__(self,...) : 构造方法 __init__(s ...

  9. python关于入参中,传入的是指针还是引用

    偶然看到别人的代码,发现有的会传入参数之后,做一次copy,试验一下,关于入参中,传入的是指针还是引用先说自己的结论:1.如果传入的是简单的类型,那么传入应该是引用的数值,2.假如传入的是df这种类型 ...

  10. js 漂浮广告

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...