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. C#线程系列讲座(1):BeginInvoke和EndInvoke方法

    一.C#线程概述 在操作系统中一个进程至少要包含一个线程,然后,在某些时候需要在同一个进程中同时执行多项任务,或是为了提供程序的性能,将要执行的任务分解成多个子任务执行.这就需要在同一个进程中开启多个 ...

  2. Java基础之在窗口中绘图——绘制圆弧和椭圆(Sketcher 3 drawing arcs and ellipses)

    控制台程序. import javax.swing.JComponent; import java.util.*; import java.awt.*; import java.awt.geom.*; ...

  3. 一款不错的多选下拉列表利器—— Ext.ux.form.SuperBoxSelect

    在B/S系统中,下拉列表(select/dropdownlist/combobox)的应用随处可见,为了增强用户体验,开发人员也常常会做一些带联想功能的下拉列表, 特别是数据项比较多的时候,用户筛选起 ...

  4. for循环、for循环嵌套

    循环:反复执行某段代码. 循环四要素:初始条件,循环条件,循环体,状态改变. 循环的最后一句:循环条件不再满足. 1.找出100以内与7有关的数并打印:(1).从1找到100(2).找出与7有关的数 ...

  5. 转:Busy Developers' Guide to HSSF and XSSF Features

    Busy Developers' Guide to Features Want to use HSSF and XSSF read and write spreadsheets in a hurry? ...

  6. 最大权闭合图最大获益(把边抽象为点)HDU3879

    题意:给出一个无向图,每个点都有点权值代表花费,每条边都有利益值,代表形成这条边就可以获得e[i]的利益,问选择那些点可以获得最大利益是多少? 分析:把边抽象成点,s与该点建边,容量是利益值,每个点与 ...

  7. 数据库SQL 多态

    Sealed关键字:密封类 该类无法被继承 部分类: Namespace 命名空间 虚拟文件夹 Partial关键字 可以将一个类拆分成多个部分,分别放在多个文件里 多态: 1.编译多态 函数重载 2 ...

  8. javascript 判断浏览器的ie版本,替换html标签

    /* var browser=navigator.appName var b_version=navigator.appVersion var version=b_version.split(&quo ...

  9. mysql之消息队列

    消息队列:在消息的传输过程中保存消息的容器. 消息队列管理器在将消息从它的源中继到它的目标时充当中间人.队列的主要目的是提供路由并保证消息的传递:如果发送消息时接收者不可用,消息队列会保留消息,直到可 ...

  10. JavaOOP项目 CMS内容管理系统

    数据库里创建一个News表,要有标题.作者.时间.内容等列. 1:首先要使用JDBC进行数据库连接,得先在项目里新建一个Folder,把Sqlserver 的驱动jar包导入. 2:使用MyEclip ...