Computer

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

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

::所有电脑连接成树形结构,要求求出每个结点到离该结点最远距离。

如果以每个结点为根结点,进行一次dfs,那就很容易求出那个最远距离。但是如果只是单纯的暴力,时间是不够的,

不过我们可以用DP的思想,避免计算重复的子问题,利用这个剪枝,效率就相当高。

我的实现方法:以每个结点为根结点root,进行一次dfs,用dp保存的是经过某条边能到达的最远距离。

边的保存是双向的,给每条边一个序号。//下面dp[x], x就是边的序号

以上图为例,首先选取任意一结点这里选1,为根结点,进行dfs,然后我们就可以求出经过边2边1..边4….所能到达的最远距离dp[1], d[2]……dp[4]..

然后以2为根结点dfs, 因为经过边4所能到达最远距离dp[4]以知道。。dp[7], dp[2]也知道,那离结点2最远结点的距离等于max(dp[4], dp[7], dp[2]+dis[3]),//dis[3]表示第三条边的长度

view code#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 10010;
int n, dp[N<<1], pre[N]; struct edge
{
int u, v, w, p;
edge() {}
edge(int u, int v, int w, int p):u(u), v(v), w(w), p(p) {}
}e[N<<1]; int ecnt = 0; int dfs(int u, int p)
{
int ans = 0;
for(int i=pre[u]; ~i; i=e[i].p)
{
int v = e[i].v;
if(v==p) continue;
if(!dp[i]) dp[i] = dfs(v, u)+e[i].w;
ans = max(ans , dp[i]);
}
return ans;
} int main()
{
// freopen("in", "r", stdin);
while(scanf("%d", &n)>0)
{
memset(dp, 0, sizeof(dp));
memset(pre, -1, sizeof(pre));
int v, w;
ecnt = 0;
for(int i=2; i<=n; i++)
{
scanf("%d%d", &v, &w);
e[ecnt] = edge(i, v, w, pre[i]);
pre[i] = ecnt++;
e[ecnt] = edge(v, i, w, pre[v]);
pre[v] = ecnt++;
}
for(int i=1; i<=n; i++)
printf("%d\n", dfs(i,-1));
}
return 0;
}

hdu 2196 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 S ...

  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. 【iOS】FMDB封装,查询自动mapping

    sqlite几乎所有的App都会用到,但是系统自带的sqlite API是用C语言写的,非常不友好,用起来非常不便,通常我们使用第三方封装好的工具,例如:FMDB(https://github.com ...

  2. window10 mysql5.7 解压版 安装

    1. 解压mysql-5.7.11-winx64.zip 到某文件夹, 如C:\DevelopCommon\mysql-5.7.11-winx64. 2. 配置环境变量 变量名 : MYSQL_HOM ...

  3. C# on Visual Studio Code

    installation Download .NET Core SDK installer and install it. https://www.microsoft.com/net/download ...

  4. 【iOS】Quartz2D图片剪切

    一.使用Quartz2D完成图片剪切1.把图片显示在自定义的view中 先把图片绘制到view上.按照原始大小,把图片绘制到一个点上. 代码: - (void)drawRect:(CGRect)rec ...

  5. Telegram传奇:俄罗斯富豪、黑客高手、极权和阴谋…

    说了很久要写Telegram的故事,一直拖延没有写.在我拖延的这段时间里面,Telegarm继续快速增长,前几天,在旧金山的TechCrunch Disrupt活动上,创始人Durov说现在Teleg ...

  6. c++中string的常用函数说明

    string可以说是是字符数组的升级版,使用更加啊方便,不容易出错.本文对string的常用函数进行简单介绍,做到会用即可. string中的常用函数分为四类,即赋值,添加,比较和删除. 一.赋值 1 ...

  7. How to Get SharePoint Client Context in SharePoint Apps (Provider Hosted / SharePoint Access ) in CSOM (Client Side Object Model)

    http://www.codeproject.com/Articles/581060/HowplustoplusGetplusSharePointplusClientplusContex Downlo ...

  8. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q28-Q31)

    Question28You have a Microsoft Office SharePoint Server 2007 site.You upgrade the site to SharePoint ...

  9. Java中==、equals、hashcode的区别与重写equals以及hashcode方法实例(转)

    Java中==.equals.hashcode的区别与重写equals以及hashcode方法实例  原文地址:http://www.cnblogs.com/luankun0214/p/4421770 ...

  10. 【转】c++中Vector等STL容器的自定义排序

    如果要自己定义STL容器的元素类最好满足STL容器对元素的要求    必须要求:     1.Copy构造函数     2.赋值=操作符     3.能够销毁对象的析构函数    另外:     1. ...