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. 如何修改myeclipse 内存?eclipse.ini中各个参数的作用。

    修改MyEclipse/eclipse文件夹中配置文件eclipse.ini中的内存分配就哦了 =================================== 一般的ini文件设置主要包括以下 ...

  2. poi 导出 excel

    private void exportAssetExcel(HttpServletRequest request, HttpServletResponse response) throws IOExc ...

  3. urllib库初体验以及中文编码问题的探讨

    提出问题:如何简单抓取一个网页的源码 解决方法:利用urllib库,抓取一个网页的源代码 ------------------------------------------------------- ...

  4. LinQ实战学习笔记(四) LINQ to Object, 常用查询操作符

    这一篇介绍了下面的内容: 查询object数组 查询强类型数组 查询泛型字典 查询字符串 SelectMany 索引 Distinct操作符 排序 嵌套查询 分组 组连接 内连接 左外连接 交叉连接 ...

  5. Web Service(一) 基础学习

    1 基础的Web Service平台是XML+HTTP. 2 Web Service平台的元素包括:SOAP(Simple Object Access Protocol)简单对象访问协议: UDDI( ...

  6. C#内存占用大量资源的解决办法

    昨天,独立完成了一个项目,一直运行起,起初运行内存为15Mb左右,但是发现内存以每秒2Mb的速度增加,吓了我一跳(注:我实习生,我的工作中第一个项目).从头找寻对象,再来dispose,弄得我晕头转向 ...

  7. android: DOC命令:查看后台运行的activity:

    DOC命令:查看后台运行的activity: adb shell dumpsys activity running activity: 模拟器曾经运行过的 activity:

  8. CSS动态伪类选择器温故-3

    动态伪类选择器 伪类选择器:大家熟悉的:[:link][:visited][:hover][:active]CSS3的伪类选择器分为六种:(1)动态伪类选择器(2)目标伪类选择器(3)语言伪类选择器( ...

  9. swift学习笔记之-函数

    //函数 import UIKit /*获得系统时间 var date = NSDate() var timeFormatter = NSDateFormatter() timeFormatter.d ...

  10. Ranges用法

    RANGES语句:要用与选择表相同的结构创建内表,可使用RANGES语句,如下所示: 语法:RANGES <seltab> FOR <f>. 该语句创建选择表<selta ...