HDU2196 Computer(树形DP)
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 32795 Accepted Submission(s): 4689
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
Recommend
lcy | We have carefully selected several similar problems for you: 1561 1011 3456 1520 2242
题目大意:
给出一棵树, 问从每个点出发所能到达的最远距离
Sol:
对于一个点,从它出发能到达的最远的距离有两种情况:
1.从它出发到子树内某个点的最长距离
2.从它的父亲边开始走能走到的最远距离
对于第一种情况,我们在转移的时候先遍历一个节点的儿子,然后在每个儿子能到达的最远距离+这条边的权值中取最大值更新
对于第二种情况,我们需要分类讨论:
如果该点是父亲的最长的儿子,那么我们需要从它的父亲所能到达的距离次大的儿子 和 父亲向上走能到达的最远距离中取max
如果该点不是父亲的最长儿子,那么我们需要从它的父亲所能到达的距离最大的儿子 和 父亲向上走能到达的最远距离中取max
因此对于一个点,我们需要维护三个量:子树中距离最大的儿子,子树中距离次大的儿子,从父边出发能到达的最远距离
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = 10001;
struct Edge {
int u, v, w, nxt;
}E[MAXN << 1];
int head[MAXN], num = 1;
inline void AddEdge(int x, int y, int z) {
E[num] = (Edge){x, y, z, head[x]};
head[x] = num++;
}
int N;
int f[MAXN][3], fa[MAXN], longson[MAXN];// 0:正向最长 1:正向次长 2:反向最长
int dfs1(int x, int _fa) {//求出点x的最长的儿子/次长的儿子
fa[x] = _fa;
for(int i = head[x], v; i != -1; i = E[i].nxt) {
if((v = E[i].v) == fa[x]) continue;
dfs1(v, x);
int val = E[i].w;
if(f[v][0] + val > f[x][0])
f[x][1] = f[x][0], f[x][0] = f[v][0] + val, longson[x] = v;
else if(f[v][0] + val > f[x][1])
f[x][1] = f[v][0] + val;
}
return f[x][0];
}
void dfs2(int x) {
for(int i = head[x], v; i != -1; i = E[i].nxt) {
if((v = E[i].v) == fa[x]) continue;
if(v == longson[x]) f[v][2] = max(f[x][2], f[x][1]) + E[i].w;
else f[v][2] = max(f[x][2], f[x][0]) + E[i].w;
dfs2(E[i].v);
}
}
int main() {
#ifdef WIN32
freopen("a.in", "r", stdin);
#endif
while(scanf("%d", &N) != EOF) {
memset(head, -1, sizeof(head)); num = 1;
memset(f, 0, sizeof(f));
for(int i = 2; i <= N; i++) {
int y, z;
scanf("%d %d", &y, &z);
AddEdge(i, y, z); AddEdge(y, i, z);
}
dfs1(1, 0);
dfs2(1);
for(int i = 1; i <= N; i++)
printf("%dn", max(f[i][0], f[i][2]));
}
return 0;
}
- 1590: [Usaco2008 Dec]Secret Message 秘密信息
- 1751: [Usaco2005 qua]Lake Counting
- 算法模板——单个值欧拉函数
- webpack前言:前端模块系统的演进
- webpack学习(一):webpack 介绍&安装&常用命令
- pd_ds中的hash
- webpack学习(二):先写几个webpack基础demo
- js实现php中sleep()延时的功能
- 洛谷P2118 比例简化(暴力)
- php 中进制之间的转换
- file_put_contents— 将一个字符串写入文件
- 跨域请求的常用方式及解释
- 洛谷P1067 多项式输出(模拟)
- php常见的判断函数
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Android简单实现弹幕效果
- 实现 Base64 的编码解码
- Android实现底部导航栏的主界面
- Spring注解@Autowired源码分析
- 使用RecyclerView实现水平列表
- android实现记住用户名和密码以及自动登录
- Android碎片fragment实现静态加载的实例代码
- android异步消息机制 源码层面彻底解析(1)
- /undefinedcss/modules/layer/default/layer.css?v=3.1.1找不到的问题
- Android实现渐变启动页和带有指示器的引导页
- the input device is not a TTY. If you are using mintty, try prefixing the comma
- 【STM32F407】第13章 RL-TCPnet V7.X之创建多个TCP客户端
- RecyclerView实现列表倒计时
- wsl设置默认账户为root(ubuntu18.04)
- LeetCode No.14 最长公共前缀