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. 2、JavaScript常用互动方法

    一.输出内容(document.write) document.write() 可用于直接向 HTML 输出流写内容.简单的说就是直接在网页中输出内容. 第一种:输出内容用“”括起,直接输出" ...

  2. python center, ljust, rjust

    例子 >>> s = "jihite" >>> s.center(, "*") '**jihite**' >>& ...

  3. iOS 自定义UITabBarController的tabBar

               #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDeleg ...

  4. (Abstract Factory)抽象工厂

    定义: 抽象工厂同工厂方法有相似处:都提供了对子类创建的封装,都是有工厂方法的接口实现类的中决定了子类被创建为什么对象. 不同于工厂方法之处:工厂方法创建的对象只是一个类型的子类,而抽象工厂创建的对象 ...

  5. 1.Spring Web MVC有什么

    Spring Web MVC使用了MVC架构模式的思想,将web层进行职责解耦. 同样也是基于请求驱动的,也就是使用请求-响应模型.它主要包含如下组件: DispatcherServlet :前端控制 ...

  6. NSDateFormatter

    NSDate *now = [NSDate date]; NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; fmt.dateFormat = ...

  7. Leetcode: Data Stream as Disjoint Intervals && Summary of TreeMap

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  8. M面经Prepare: Delete Words Starting With One Character

    给定一个char array, 这个array是一个句子,然后给定一个字母,把这个array里面带有这个字母开头的单次删掉,操作是要求in place. 检测   array[i]==' ' & ...

  9. JAVA多线程与多进程

    并发与并行是两个既相似而又不相同的概念,但往往容易混为一谈,这两者究竟有什么区别呢?本文通过一个例子让你更好地理解(本文由并发编程网翻译). 现代社会是并行的:多核.网络.云计算.用户负载,并发技术对 ...

  10. 使用Mysql修改密码命令更改root的密码

    使用Mysql修改密码命令更改root的密码. 进入Mysql数据库命令行方式有两种方式: 方式一:在Mysql开始菜单里包含Mysql命令行客户端,只要点击输入root的密码即可进入. 方式二:在D ...