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. JS 将一段文本 每个英文首字母大写

    function replaceStr(str){ // 正则法 str = str.toLowerCase(); var reg = /\b(\w)|\s(\w)/g; // \b判断边界\s判断空 ...

  2. php查询IP地址归属等信息

    淘宝公司提供了一个很好用的IP地理信息查询接口.在这里:http://ip.taobao.com/TaobaoIPQuery2这个类将极大的简化相关的信息查询. 类 TaobaoIPQuery2 文件 ...

  3. 百度地图刷新显示不完整?(应该是和div顺序有关系)

    解决方案:1异步加载(jquery(function(){loadJScript():}))   2解析加载设置了个延迟(setTimeOut(getInit,1000))

  4. 基于Eclipse的Go语言可视化开发环境

    http://jingyan.baidu.com/article/d7130635032e2f13fdf475b8.html 基于Eclipse的Go语言可视化开发环境 | 浏览:2924 | 更新: ...

  5. RAID选项

    RAID:Redundant Array Independent Disk(独立磁盘构成的具有冗余能力的阵列) 最常见的为RAID类型为:0,1,5和10:3和6很少见,但在某些环境中仍然有用. RA ...

  6. Android应用开发基础之九:内容提供者(ContentProvider)

    内容提供者 应用的数据库是不允许其他应用访问的 内容提供者的作用:就是让别的应用访问到你的数据库 自定义内容提供者,继承ContentProvider类,重写增删改查方法,在方法中写增删改查数据库的代 ...

  7. IOS缓存机制详解

    资料均来自互联网,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任. 人魔七七:http://www.cnblogs.com/qiqibo/ 为什么要有缓存 应用需要 ...

  8. 利用jQuery的淡入淡出实现轮播器

    基本原理:将所有图片绝对定位在同一位置,透明度设为0,然后通过jQuery的淡入淡出实现图片的切换效果: 但我在使用fadeIn淡入时却无效果,最后只能使用fadeTo实现,求大神指教 HTML: & ...

  9. gridView使用

    只读 for (int i = 0; i <9; i++) { this.gridView1.Columns[i].OptionsColumn.ReadOnly = true; } 不显示面板 ...

  10. 移动,企业社交(sharepoint2013)--jindahao(金大昊)

    MobileIncreasingly, a major component of sharing and collaborating involves mobile access. SharePoin ...