Computer

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2850    Accepted Submission(s): 1450

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
代码:
 /***
分析:以编号的i的节点为例(非根节点),最长的路径长度只有俩种可能,
1)子树中存在最长路径;
2)通过父节点的路径中存在最长路径
所以,只有分别求出每一节点对应的那俩种路径取大最大值即可,其中,根节点只存在第一种可能
***/
#include "stdio.h"
#include "string.h" #define N 10005 struct node{
int x,y;
int weight;
int next;
}edge[*N];
int idx,head[N]; void Init(){idx=; memset(head,-,sizeof(head));} void Add(int x,int y,int weight)
{
edge[idx].x = x;
edge[idx].y = y;
edge[idx].weight = weight;
edge[idx].next = head[x];
head[x] = idx++;
} struct point{
int id;
int value;
}dp1[N],dp2[N]; //dp1[i]记录点i的最远距离,dp2[i]记录点i的次远距离, void swap(point &a,point &b)
{
point c;
c = a;
a = b;
b = c;
} void DFS1(int x,int father)
{
int i,y;
dp1[x].value = dp2[x].value = ;
for(i=head[x]; i!=-; i=edge[i].next)
{
y = edge[i].y;
if(y==father) continue;
DFS1(y,x);
if(dp1[y].value + edge[i].weight > dp2[x].value)
{
dp2[x].value = dp1[y].value + edge[i].weight;
dp2[x].id = y;
if(dp1[x].value < dp2[x].value) //dp1[i]记录点i的最远距离,dp2[i]记录点i的次远距离,
swap(dp1[x],dp2[x]);
}
}
} void DFS2(int x,int father)
{
int i,y;
for(i=head[x]; i!=-; i=edge[i].next)
{
y = edge[i].y;
if(y==father) continue;
if(dp1[x].id == y) //点y是父亲x的最远距离的下一个节点
{
if(dp2[y].value < dp2[x].value+edge[i].weight) //,那么看点y的次元距离能否通过父亲x的其他节点更新
{
dp2[y].value = dp2[x].value + edge[i].weight;
dp2[y].id = x;
if(dp1[y].value < dp2[y].value)
swap(dp1[y],dp2[y]);
}
}
else
{
if(dp2[y].value < dp1[x].value+edge[i].weight)
{
dp2[y].value = dp1[x].value+edge[i].weight;
dp2[y].id = x;
if(dp1[y].value < dp2[y].value)
swap(dp1[y],dp2[y]);
}
}
DFS2(y,x);
}
} int main()
{
int i,n;
int x,y,k;
while(scanf("%d",&n)!=EOF)
{
Init();
for(y=; y<=n; ++y)
{
scanf("%d %d",&x,&k);
Add(x,y,k);
Add(y,x,k);
}
DFS1(,-);
DFS2(,-);
for(i=; i<=n; ++i)
printf("%d\n",dp1[i].value);
}
return ;
}
 

hdu 2196 Computer 树形dp模板题的更多相关文章

  1. HDU 2196 Computer 树形DP经典题

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

  2. HDU 2196 Computer 树形DP 经典题

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

  3. HDU 2196.Computer 树形dp 树的直径

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

  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 Su ...

  6. HDU 2196 Computer (树dp)

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

  7. HDU - 2196(树形DP)

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

  8. hdu 2196【树形dp】

    http://acm.hdu.edu.cn/showproblem.php?pid=2196 题意:找出树中每个节点到其它点的最远距离. 题解: 首先这是一棵树,对于节点v来说,它到达其它点的最远距离 ...

  9. HDU 2196 Compute --树形dp

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

随机推荐

  1. nodejs+express+jade给我baby做个小相册

    去年年底迎来了my little star.从此人生多了一个最重要的牵挂.生了宝宝全家人都太忙了.最近宝宝稍微大点了,终于有空可以研究下技术了.这是14年第一帖.废话不多了.开始吧 1.安装NTVS ...

  2. (转载)IO-同步、异步、阻塞、非阻塞

    一.概述 同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非阻塞(non-blocking)IO分别是什么,到底有什么区别?这个问题其实不 ...

  3. 【C#进阶系列】00 序

    老早就被各种推荐<CLR via C#>这本书了,然而一直没去学. 因为工作中所需要的.NET功底目前算是足以应付了,而前端却不熟,所以跑去学了一段时间前端的知识. 终于算是把前端方面的基 ...

  4. 重新想象 Windows 8 Store Apps (41) - 打印

    [源码下载] 重新想象 Windows 8 Store Apps (41) - 打印 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 打印 示例1.需要打印的文档Pr ...

  5. PHP 操作mongodb api大部分方法

    <?php /* PHP mongodb * 全部curd操作 * @author:xiaojiang * @date: 2014-10-27 */ //查看 mongo类版本 1.30 以后版 ...

  6. postgresql 9.6 rc1发布

    postgresql 9.6 rc1发布了,意味着postgresql 9.6正式版将会越来越近了. 对于dss来说,postgresql远优于mysql,尤其是9.6新引入的并行执行,将大大提高性能 ...

  7. Play Framework介绍:控制器层

    业务逻辑代码通常位于模型(model)层.客户端(比如浏览器)无法直接调用其中的代码,所以模型对象提供的功能,必须作为资源以URI方式暴露给外部. 客户端使用HTTP协议来操作这些资源,从而调用了内部 ...

  8. AngularJS directive 指令相关记录

    .... .directive('scopeDemo',function(){ return{ template: "<div class='panel-body'>Name: ...

  9. cl_gui_cfw=>dispatch

    将已经触发的EVENT发送给他们各自的EVENT HANDLER,以便让这些事件得到响应. 根据返回值可以判断是否发送成功. CALL METHOD cl_gui_cfw=>dispatch   ...

  10. HBase体系结构剖析

    本文出自:http://wuyudong.com/archives/154 在上篇文章<HBase简介>中,已经提到过,HBase中的Table中的所有行都按照row key的字典序排列, ...