http://acm.split.hdu.edu.cn/showproblem.php?pid=2196

Computer
 
 

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 题意:给出一棵树,求树中每个节点到树中任意其他节点的最大距离。
思路:想了挺久还是不会做,只能学习一下别人的了。http://blog.csdn.net/shuangde800/article/details/9732825
   和之前那道水题完全天壤之别。
   用两次dfs来求出最大的距离。第一次dfs是求出节点i的子树节点到i的最大距离,用dp[i][0]表示(从上往下)。第二次dfs是求出不在节点i的子树的节点中的其他节点到节点i的最大距离(从下往上),即到父节点的最大距离 + 父节点和该节点的w(父节点的最大距离的路径不能包含i,若包含要用次大距离),用dp[i][1]表示。答案就取max(dp[i][0], dp[i][1]).
 #include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
#define N 10010
struct node
{
int v, nxt, w;
}edge[N*];
int head[N], tot;
int dp[N][];
bool vis[N]; /*
dp[i][0],表示顶点为i的子树的,距顶点i的最长距离
dp[i][1],表示Tree(i的父节点)-Tree(i)的最长距离+i跟i的父节点距离
Tree(x)表示以x为根的子树
*/
void add(int u, int v, int w)
{
edge[tot].v = v;
edge[tot].w = w;
edge[tot].nxt = head[u];
head[u] = tot++;
edge[tot].v = u;
edge[tot].w = w;
edge[tot].nxt = head[v];
head[v] = tot++;
} void dfs1(int u) //回溯的时候找到各个节点子树距节点的最大距离
{
vis[u] = ;
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v, w = edge[i].w;
if(vis[v]) continue;
dfs1(v);
dp[u][] = max(dp[u][], w + dp[v][]);
}
} void dfs2(int u) //从上往下找不属于该节点子树的节点到该节点的最大距离,即从另一个方向找
{
vis[u] = ;
int ma1 = , ma2 = , tmp, v1, v2;
for(int i = head[u]; ~i; i = edge[i].nxt) { //从子节点到该节点父节点最大距离
int v = edge[i].v, w = edge[i].w;
if(vis[v]) continue;
tmp = dp[v][] + w;
if(tmp > ma1) {
ma2 = ma1, v2 = v1, ma1 = tmp, v1 = v;
} else if(tmp > ma2) {
ma2 = tmp, v2 = v;
}
} if(u != ) { //有父节点有兄弟的话,找从其他节点到父节点的最长距离
tmp = dp[u][];
int v = ; //这个时候一定是用dp[u][1],所以不受限制
if(tmp > ma1) {
ma2 = ma1, v2 = v1, ma1 = tmp, v1 = v;
} else if(tmp > ma2) {
ma2 = tmp, v2 = v;
}
} for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v, w = edge[i].w;
if(vis[v]) continue;
if(v == v1) { //如果最长的距离的路径经过该节点,那么只能选次大的
dp[v][] = ma2 + w;
} else { //否则可以选最大的
dp[v][] = ma1 + w;
}
dfs2(v);
}
} int main()
{
int n;
while(~scanf("%d", &n)) {
memset(head, -, sizeof(head));
tot = ;
for(int i = ; i <= n; i++) {
int v, w;
scanf("%d%d", &v, &w);
add(i, v, w);
} memset(vis, , sizeof(vis));
memset(dp, , sizeof(dp));
dfs1();
memset(vis, , sizeof(vis));
dfs2(); for(int i = ; i <= n; i++) {
printf("%d\n", max(dp[i][], dp[i][]));
}
} return ;
}
 

HDU 2136: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. HDU 2196 Computer 树形DP经典题

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

  3. HDU 2196 Computer 树形DP 经典题

    给出一棵树,边有权值,求出离每一个节点最远的点的距离 树形DP,经典题 本来这道题是无根树,可以随意选择root, 但是根据输入数据的方式,选择root=1明显可以方便很多. 我们先把边权转化为点权, ...

  4. hdu 2196 Computer(树形DP)

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

  5. hdu 2196 Computer 树形dp模板题

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

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

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

  7. HDU 2196 Computer (树dp)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2196 给你n个点,n-1条边,然后给你每条边的权值.输出每个点能对应其他点的最远距离是多少 ...

  8. HDU - 2196(树形DP)

    题目: A school bought the first computer some time ago(so this computer's id is 1). During the recent ...

  9. computer(树形dp || 树的直径)

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

  10. hdu 6201 【树形dp||SPFA最长路】

    http://acm.hdu.edu.cn/showproblem.php?pid=6201 n个城市都在卖一种书,该书的价格在i城市为cost[i],商人打算从某个城市出发到另一个城市结束,途中可以 ...

随机推荐

  1. ISymbol

    public void Draw (IGeometry Geometry); public void QueryBoundary (     int hDC,     ITransformation ...

  2. javaScript学习之ajax

    一.xmlHttpRequest对象的创建 ajax的核心的XMLHttpRequest对象,下面的代码给出了兼容各个浏览器的方法实现 function createXHR(){ if(typeof ...

  3. mysql 启动服务

    http://blog.chinaunix.net/uid-13642598-id-3153537.html mysql的四种启动方式: 1.mysqld 启动mysql服务器:./mysqld -- ...

  4. SQL 启动服务方法

    (1)windows开始菜单->Microsoft SQL Server 2012->配置工具->配置管理器

  5. sdutoj 2604 Thrall’s Dream

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2604 Thrall’s Dream Time ...

  6. 常见http代码错误原因及处理

    常见的HTTP错误可以分为以下四大类.每一大类又细分为很多类小错误.当您打不开网站或者打开网站报错时首先检查您输入的网站是否有误,检查网络是否有问题或者虚拟主机的DNS是否可以解析.确定没有问题时再看 ...

  7. POJ 1811 Prime Test(Miller-Rabin & Pollard-rho素数测试)

    Description Given a big integer number, you are required to find out whether it's a prime number. In ...

  8. ligerUI_入门_001_设置文本能否被编辑、事件

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. Android 沉浸式状态栏

    1,传统的手机状态栏是呈现出黑色或者白色条状的,有的和手机主界面有很明显的区别.这样就在一定程度上牺牲了视觉宽度,界面面积变小.看一下QQ的应用 2,实现起来也挺简单的,来一起看一下吧 MainAct ...

  10. 04---Net基础加强

    字符串常用方法: 属性: Length获取字符串中字符的个数 IsNullOrEmpty()   静态方法,判断为null或者为“” ToCharArray() 将string转换为char[] To ...