Computer

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 31049    Accepted Submission(s): 3929

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
 
Author
scnu
 
题意:求一棵树上任一点到其他点的最远距离。
思路:1)树上的任意一点到其他点的最远距离必然是到达树的直径的两个端点之一,树的直径只需要2遍dfs即可。
2)重点说一下树形dp的思路。dp[i][0],dp[i][1],dp[i][2]分别表示:i通过子树可以达到的最远距离;i通过子树可以到达的次远距离(不同儿子);i通过父亲能够到达的最远距离。那么ans[i]=max(dp[i][0],dp[i][2]);
对于<u,v>w:
状态转移方程:dp[u][0]=dp[v][0]+w;dp[u][1]也是这样得到,但是注意dp[i][1]是通过另外的儿子获得。
1.dp[v][2]=dp[u][1]+w; 2.dp[v][2]=dp[u][0]+w;当<u,v> 是u的最长子树的路径时,只能用1式子。
代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<algorithm>
  6. #include<queue>
  7. #include<stack>
  8. #include<map>
  9. #include<stack>
  10. #include<set>
  11. #include<bitset>
  12. using namespace std;
  13. #define PI acos(-1.0)
  14. #define eps 1e-8
  15. typedef long long ll;
  16. typedef pair<int,int > P;
  17. const int N=1e5+,M=1e5+;
  18. const int inf=0x3f3f3f3f;
  19. const ll INF=1e18+,mod=1e9+;
  20. struct edge
  21. {
  22. int from,to;
  23. ll w;
  24. int next;
  25. };
  26. edge es[M];
  27. int cnt,head[N];
  28. ll dp[N][];
  29. void init()
  30. {
  31. cnt=;
  32. memset(head,-,sizeof(head));
  33. }
  34. void addedge(int u,int v,ll w)
  35. {
  36. cnt++;
  37. es[cnt].from=u,es[cnt].to=v;
  38. es[cnt].w=w;
  39. es[cnt].next=head[u];
  40. head[u]=cnt;
  41. }
  42. void dfs1(int u,int fa)
  43. {
  44. for(int i=head[u]; i!=-; i=es[i].next)
  45. {
  46. edge e=es[i];
  47. if(e.to==fa) continue;
  48. dfs1(e.to,u);
  49. ll d=dp[e.to][]+e.w;
  50. if(d>dp[u][]) swap(d,dp[u][]);
  51. if(d>dp[u][]) swap(d,dp[u][]);
  52. }
  53. }
  54. void dfs2(int u,int fa)
  55. {
  56. for(int i=head[u]; i!=-; i=es[i].next)
  57. {
  58. edge e=es[i];
  59. if(e.to==fa) continue;
  60. if(dp[u][]==dp[e.to][]+e.w)
  61. dp[e.to][]=max(dp[u][],dp[u][])+e.w;
  62. else
  63. dp[e.to][]=max(dp[u][],dp[u][])+e.w;
  64. dfs2(e.to,u);
  65. }
  66. }
  67. int main()
  68. {
  69. int n;
  70. while(~scanf("%d",&n))
  71. {
  72. init();
  73. for(int i=,j; i<=n; i++)
  74. {
  75. ll w;
  76. scanf("%d%lld",&j,&w);
  77. addedge(i,j,w);
  78. addedge(j,i,w);
  79. }
  80. memset(dp,,sizeof(dp));
  81. dfs1(,);
  82. dfs2(,);
  83. for(int i=; i<=n; i++)
  84. printf("%d\n",max(dp[i][],dp[i][]));
  85. }
  86. return ;
  87. }

树形dp

HDU 2196.Computer 树形dp 树的直径的更多相关文章

  1. HDU 2196 Computer 树形DP经典题

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

  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-2169 Computer(树形dp+树的直径)

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

  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模板题

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

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

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

  8. Computer(HDU2196+树形dp+树的直径)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2196 题目: 题意:有n台电脑,每台电脑连接其他电脑,第i行(包括第一行的n)连接u,长度为w,问你每 ...

  9. hdu 4607 树形dp 树的直径

    题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n)个点,至少需要走多少距离(每条边的距离是1): 思路:树形dp求树的直径r: a:若k<=r+1 ...

随机推荐

  1. git 入门与应用

    git可视化界面的项目版本控制软件,适用于git项目管理 SourceTree 安装方法 https://blog.csdn.net/qq_26230421/article/details/79921 ...

  2. hive join on 条件 与 where 条件区别

    1. select * from a left join b on a.id = b.id and a.dt=20181115; 2. select * from a left join b on a ...

  3. 在虚拟机上的ubuntu 1.6 系统中sudo apt-get失败的问题

    在虚拟机上sudo apt-get update 失败.可能是网络dns问题,把nameserver \设为你路由器的内网ip地址就没事了; 详细: 1/打开sudo gedit /etc/resol ...

  4. cached_property的使用

    cached_property修饰过的函数,变成是对象的属性,该对象第一次引用该属性时,会调用函数,对象第二次引用该属性时就直接从词典中取了,这也说明引用属性是经过__getattritue__. c ...

  5. java面试题复习(三)

    21.静态嵌套类和内部类的不同? 答:静态嵌套类是被声明为静态(static)的内部类,它可以不依赖于外部类实例被实例化.而通常的内部类需要在外部类实例化后才能实例化.//还是考的static的知识 ...

  6. sublime text3 离线安装插件方法 package control

    package control 在线安装 一般会出现各种错误 不推荐 离线安装 推荐step1: 打开package control官网https://packagecontrol.io/ step2 ...

  7. 路由对象route

    路由对象是不可变 (immutable) 的,每次成功的导航后都会产生一个新的对象.不过你可以 watch (监测变化) 它. 通过 this.$route 访问当前路由,还可以通过router.ma ...

  8. python module -- sys

    sys模块主要是用于提供对python解释器相关的操作 http://www.cnblogs.com/pycode/p/sysos.html http://blog.csdn.net/pipisorr ...

  9. 36_react_ui_antd

    1:最流行的开源react ui组件库 1.1:material-ui(国外) 1.2:ant-design(推荐:国内蚂蚁金服) 2.如何使用 方式一(页面引入): 在<head>标签内 ...

  10. Java学习笔记 -- Java定时调度工具Timer类

    1 关于 (时间宝贵的小姐姐请跳过) 本教程是基于Java定时任务调度工具详解之Timer篇的学习笔记. 什么是定时任务调度 基于给定的时间点,给定的时间间隔或者给定的执行次数自动执行的任务. 在Ja ...