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.

InputInput
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.OutputFor 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

题意 : 给你一颗树,以及树上两点之间的距离,求任意一点所能到的最远的距离。

思路分析:

  对于这个问题,我们可以这样去思考,对于一颗树可以很方便的求出 以当前节点为根节点其所能到达的最远距离,但是这样并不能得到所有的节点的答案,其他的节点需要以其再考虑一下当前节点向上走的情况

再考虑 2 这个节点的时候其最优值可能来自 2 这个子树,也可能来自于右侧的这颗红色的树,即向上走

dp[x][0] 表示 x 节点向下走的最大值, dp[x][1]表示 x 节点向下走的次大值, dp[x][2] 表示 x 节点向上走的最大值。

代码示例:

  1. const int maxn = 1e4+5;
  2.  
  3. int n;
  4. struct node
  5. {
  6. int to, cost;
  7.  
  8. node(int _to=0, int _cost=0):to(_to), cost(_cost){}
  9. };
  10. vector<node>ve[maxn];
  11. int dp[maxn][3];
  12. int p[maxn];
  13.  
  14. void dfs1(int x, int fa){
  15.  
  16. for(int i = 0; i < ve[x].size(); i++){
  17. int to = ve[x][i].to;
  18. int cost = ve[x][i].cost;
  19. if (to == fa) continue;
  20.  
  21. dfs1(to, x);
  22. if (dp[x][0] <= dp[to][0]+cost){
  23. dp[x][1] = dp[x][0];
  24. dp[x][0] = dp[to][0]+cost;
  25. p[x] = to;
  26. }
  27. else if (dp[x][1] < dp[to][0]+cost){
  28. dp[x][1] = dp[to][0]+cost;
  29. }
  30. }
  31. }
  32. void dfs2(int x, int fa){
  33.  
  34. for(int i = 0; i < ve[x].size(); i++){
  35. int to = ve[x][i].to;
  36. int cost = ve[x][i].cost;
  37. if (to == fa) continue;
  38.  
  39. if (to != p[x]){
  40. dp[to][2] = max(dp[x][0]+cost, dp[x][2]+cost);
  41. }
  42. else dp[to][2] = max(dp[x][1]+cost, dp[x][2]+cost);
  43. dfs2(to, x);
  44.  
  45. //printf("++++ %d %d %d \n", x, to, dp[to][2]);
  46. }
  47. }
  48.  
  49. int main() {
  50. //freopen("in.txt", "r", stdin);
  51. //freopen("out.txt", "w", stdout);
  52. int x, y;
  53. while(~scanf("%d", &n)){
  54. for(int i = 1; i <= n; i++) ve[i].clear();
  55. for(int i = 2; i <= n; i++){
  56. scanf("%d%d", &x, &y);
  57. ve[i].push_back(node(x, y));
  58. ve[x].push_back(node(i, y));
  59. }
  60. memset(dp, 0, sizeof(dp));
  61. dfs1(1, 0);
  62. dfs2(1, 0);
  63.  
  64. for(int i = 1; i <= n; i++) {
  65. printf("%d\n", max(dp[i][0], dp[i][2]));
  66. }
  67. }
  68.  
  69. return 0;
  70. }

求树上任意一点所能到达的最远距离 - 树上dp的更多相关文章

  1. xdoj-1319 求树上任意一点的最大距离----利用树的直径

    1 #include <bits/stdc++.h> using namespace std; ; vector < vector <int> > g(N); in ...

  2. hdu-2196 树形dp 求一个树中所有节点能到达的最远距离f[i] (其实也不难嘛!)

    #include <bits/stdc++.h> using namespace std; ; struct T { int to; int w; }; vector < vecto ...

  3. HDU 2196 求树上所有点能到达的最远距离

    其实我不是想做这道题的...只是今天考试考了一道类似的题...然后我挂了... 但是乱搞一下还是有80分....可惜没想到正解啊! 所以今天的考试题是: 巡访 (path.pas/c/cpp) Cha ...

  4. Bellman_Ford算法(求一个点到任意一点的最短距离)

    单源最短路问题是固定一个起点,求它到任意一点最短路的问题. 记从起点出发到顶点 i 的最短距离为d[i],则有以下等式成立 d[i]=min{d[j]+(从j到 i 的边的权值) 看代码 #inclu ...

  5. HDU 2376 树形dp|树上任意两点距离和的平均值

    原题:http://acm.hdu.edu.cn/showproblem.php?pid=2376 经典问题,求的是树上任意两点和的平均值. 这里我们不能枚举点,这样n^2的复杂度.我们可以枚举每一条 ...

  6. caioj 1237: 【最近公共祖先】树上任意两点的距离 在线倍增ST

    caioj 1237: [最近公共祖先]树上任意两点的距离 倍增ST 题目链接:http://caioj.cn/problem.php?id=1237 思路: 针对询问次数多的时候,采取倍增求取LCA ...

  7. 关于delphi点击webbrowser中任意一点的问题

    关于delphi点击webbrowser中任意一点的问题 有时候我们需要delphi载入webbrowser1打开网页的时候 需要点击某一个点的位置 可能是坐标 可能是按钮 可能是其他的控件应该如何来 ...

  8. echarts 点击方法总结,点任意一点获取点击数据,在多图联动中用生成标线举例

    关于点击(包括左击,双击,右击等)echarts图形任意一点,获取相关的图形数据,尤其是多图,我想部分人遇到这个问题一直很头大.下面我用举例说明,如何在多图联动基础上,我们点击点击任意一个图上任意一点 ...

  9. HDU 5723 Abandoned country(kruskal+dp树上任意两点距离和)

    Problem DescriptionAn abandoned country has n(n≤100000) villages which are numbered from 1 to n. Sin ...

随机推荐

  1. 使用原生JS封装一个动画函数

    最近一直在忙项目,很少有时间回顾之前的知识,今天刚好要做一个轮播,因为对兼容性有一定的要求,使用了各种插件和库中的轮播,效果都不是很理想,一怒之下,使用原生JS封装了一个轮播组件,其中重要的功能就是一 ...

  2. Codeforces Round #183 (Div. 2)

    A. Pythagorean Theorem II 暴力,\(O(n^2)\). B. Calendar 每个日期计算到0年1月1日的天数,相当于转化成前缀和形式. 闰年数计算\[\lfloor\fr ...

  3. vue-cil 打包爬坑(解决)

    1.打包成功不报错,但是打开页面啥都没有?  解决:文件位置:config目录下index.js文件更改assetsPublicPath属性:文件里面有两个assetsPublicPath属性,也就是 ...

  4. H3C 迭代查询

  5. Webstorm 配置 Less编译

    配置less编译

  6. H3C开启Ssh

    [H3C]ssh server enable                       //开启SSH服务 [H3C]user-interface vty 0 4 [H3C-ui-vty0-4]au ...

  7. vue-learning:9-template-v-model

    表单元素的双向绑定指令v-model 目录 v-model的基础用法 v-model双向绑定实现的原理 v-model绑定值的输出类型(字符串.数组.布尔值.自定义) v-model修饰符:.lazy ...

  8. SRTE测试

    网络拓扑: XRV1 ======================================================================= hostname XRV1expl ...

  9. linux下文件的一些文件颜色的含义

    红色---->代表压缩文件 红色闪烁---->代表链接文件有问题 黄色---->代表设备文件 深蓝色---->代表目录 浅蓝色----->代表链接文件 绿色----> ...

  10. window10 自带虚拟机输入ip addr 不显示ip,显示字母加数字

    \(\color{Black}{文/魂皓轩}\) 1.在界面输入ip addr 2.通过ls 查看当前文件 我的虚拟机网络配置文件为ifcfg-eth0(不同主机文件名不一样) 3.通过 vi ifc ...