题目大意:给N个点,求每个点的与其他点距离最大值

很经典的树形dp...很久前就想写来着...看了陈老师的code才会的...mx[x][0], mx[x][1]分别表示x点子树里最长的2个距离, dfs一遍得到. mx[x][2]表示从x的父亲到x的最长路径长度, 也是dfs一遍得到(具体看代码)。最后答案就是max(mx[x][0], mx[x][2]). 时间复杂度O(N)

--------------------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
 
using namespace std;
 
const int maxn = 10009;
 
int N, mx[maxn][3];
 
struct edge {
int to, w;
edge* next;
} E[maxn << 1], *pt = E, *head[maxn];
 
void AddEdge(int u, int v, int w) {
pt->to = v; pt->w = w; pt->next = head[u]; head[u] = pt++;
}
 
void Init() {
scanf("%d", &N);
for(int i = 1; i < N; i++) {
int t, w; scanf("%d%d", &t, &w); t--;
AddEdge(i, t, w);
AddEdge(t, i, w);
}
}
 
inline void upd(int x, int t) {
if(mx[x][1] < t)
mx[x][1] = t;
if(mx[x][0] < mx[x][1])
swap(mx[x][0], mx[x][1]);
}
 
void DFS0(int x, int fa = -1) {
mx[x][0] = mx[x][1] = 0;
for(edge* e = head[x]; e; e = e->next) if(e->to != fa) {
DFS0(e->to, x);
upd(x, mx[e->to][0] + e->w);
}
}
 
void DFS1(int x, int fa = - 1) {
for(edge* e = head[x]; e; e = e->next) if(e->to != fa) {
mx[e->to][2] = mx[x][2] + e->w;
if(mx[e->to][0] + e->w == mx[x][0])
mx[e->to][2] = max(mx[e->to][2], mx[x][1] + e->w);
else
mx[e->to][2] = max(mx[e->to][2], mx[x][0] + e->w);
DFS1(e->to, x);
}
}
 
int main() {
Init();
DFS0(0);
DFS1(mx[0][2] = 0);
for(int i = 0; i < N; i++)
printf("%d\n", max(mx[i][2], mx[i][0]));
return 0;
}

--------------------------------------------------------------------------------------------

149. Computer Network

time limit per test: 0.25 sec.
memory limit per test: 4096 KB
input: standard input
output: standard output

A school bought the first computer some time ago. 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 for each computer number Si - maximum distance, 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.
Input
There is natural number N (N<=10000) in the first line of input, 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
Write N lines in output file. i-th line must contain number Si for i-th computer (1<=i<=N).
Sample test(s)
Input


1 1 
1 2
Output



3

Author: Andrew V. Lazarev, Michael R. Mirzayanov
Resource: Saratov Subregional School Team Contest, 2002
Date: Fall, 2002

最新文章

  1. AJAX操作数据
  2. 截取UTF-8编码的汉字,最后一个字出现乱码的问题
  3. css3中transition和display的坑
  4. [python实现设计模式]-3.简单工厂模式-触宝开放平台
  5. java web 学习 --第五天(Java三级考试)
  6. 主机与虚拟机通信:以主机VS2010连接虚拟机MySql为例
  7. php-fpm 进程管理
  8. lnmp下配置虚拟主机
  9. Linux Kernel ‘exitcode_proc_write()’函数本地缓冲区溢出漏洞
  10. 窥探Unity5渲染内部之解析UnityShaderVariables.cginc
  11. Javascript 获取url参数,hash值 ,cookie
  12. Ubuntu12.10无法安装openssh-server[已解决]
  13. WTL 设置 SDI 主窗口初始大小的方法
  14. 搭建自己的CA服务 - OpenSSL CA 实战
  15. ava 8中的新功能特性
  16. 读取Excel中数据
  17. C++了解free和delete
  18. 02 爬虫数据解析之re,xpath,beautifulsoup
  19. springboot分环境打包(maven动态选择环境)
  20. Python学习-39.Python中的生成器

热门文章

  1. IEnumerable和IEnumerator 详解 【转】
  2. Swift应用开源项目推荐
  3. jQuery源码笔记——延迟对象
  4. RESTful API学习与实践
  5. js——BOM
  6. emoji表情字符串 mysql 普通 utf8 格式无法存入
  7. jquery结合highcharts插件显示实时数据动态曲线图
  8. 在centos7下安装mysql5.7
  9. 软件测试学习日志————round 0 An impressed error in my past projects
  10. Junit技巧