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

  1. 5
  2. 1 1
  3. 2 1
  4. 3 1
  5. 1 1

Sample Output

  1. 3
  2. 2
  3. 3
  4. 4
  5. 4
    题目大意:
    给定n,代表n台电脑,编号为1的是最初始的电脑,下面n-1对数字,分别编号为2~n的电脑相连电脑的编号与长度。
    输出n行,求与第i台电脑最远的电脑的距离。
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstring>
  4. using namespace std;
  5. int n,dp[],pre[];
  6. struct edge
  7. {
  8. int u,v,w,p;///u与v电脑相连,长度w,u对应的上一条边
  9. edge(){}
  10. edge(int u,int v,int w,int p):u(u),v(v),w(w),p(p){}
  11. }e[];
  12. int dfs(int u,int p)
  13. {
  14. int ans=;
  15. for(int i=pre[u];i!=-;i=e[i].p)
  16. {
  17. int v=e[i].v;
  18. if(v==p) continue;
  19. if(!dp[i])///没更新过
  20. dp[i]=dfs(v,u)+e[i].w;
  21. ans=max(ans,dp[i]);
  22.  
  23. }
  24. return ans;
  25. }
  26. int main()
  27. {
  28. while(cin>>n)
  29. {
  30. memset(dp,,sizeof dp);
  31. memset(pre,-,sizeof pre);
  32. int x=;
  33. for(int i=,v,w;i<=n;i++)
  34. {
  35. cin>>v>>w;
  36. e[x]=edge(i,v,w,pre[i]);
  37. pre[i]=x++;
  38. e[x]=edge(v,i,w,pre[v]);
  39. pre[v]=x++;
  40. }
  41. for(int i=;i<=n;i++)
  42. cout<<dfs(i,-)<<'\n';
  43. }
  44. return ;
  45. }
  1.  

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. 动手实现 Redux(五):不要问为什么的 reducer

    经过了这么多节的优化,我们有了一个很通用的 createStore: function createStore (state, stateChanger) { const listeners = [] ...

  2. CF983A Finite or not?

    思路: 如果p,q不互质,先把q除以p,q的最大公约数.接下来只要b中包含了所有q的质因子就可以了.可以在gcd(q, b) 不等于1的时候不断地用q除以gcd(q, b).最后如果q等于1,说明Fi ...

  3. jstat查看JVM的GC情况

    jps(Java Virtual Machine Process Status Tool)是JDK 1.5提供的一个显示当前所有java进程pid的命令,简单实用,非常适合在linux/unix平台上 ...

  4. System类与两种输入流

    1.System类对I/O的支持系统输出System.out.println 是利用了I/O流的模式完成的.实际是打印流PrintStream对象 System类中定义了三个操作的常量 1.标准/系统 ...

  5. Fire Air(华科校赛 网络赛)

    题目 原题链接:https://www.nowcoder.com/acm/contest/106/L 在100000 * 10000的空地上,有n个时间点,每个时间点会在(xi,yi)上种一棵树. 定 ...

  6. uva11491 Erasing and Winning

    边读入边处理 优化了速度一开始有想错了的地方.处理输入有点想用stringstream, 的问题在于他把字符串连续的数字作为一个整体,遇到空格才分开,所以不适用 #include<cstdio& ...

  7. 【转载】用Python实现端口映射功能(A/B/C内外网)

    转载地址 :http://hutaow.com/blog/2014/09/08/write-tcp-mapping-program-with-python/ 有A,B,C三台计算机,A,B互通,B,C ...

  8. 全志T8智能汽车方案芯片参数介绍

    T8处理器代表了Allwinner在智能汽车市场上的最新成就.T8适用于需要三维图形.高级视频处理.精密相机.多种连接选项和高水平系统集成的应用程序.它将把先进的消费电子体验带入未来的汽车,实现高性能 ...

  9. 【2019.6.2】python:json操作、函数、集合、random()等

    一.json操作: json就是一个字符串,从文件中读取json,必须是json格式.j'son串中必须是双引号,不能有单引号,单引号不能转换 1.1使用: import json #使用json先引 ...

  10. 6 SQL 函数、谓词、CASE表达式

    6 函数.谓词.CASE表达式 6-1 各种各样的函数 /* 所谓函数,就是输入某一值得到相应输出结果的功能.输入值称为参数(parameter),输出值称为返回值. 函数大致可以分为以下几种 : 算 ...