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. Oracle工程建设行业解决方案

    为何选择Oracle工程建设行业解决方案? Oracle为工程建设企业提供一套全面.开放且集成的业务管理软件.服务器和存储解决方案.这些解决方案经过集成设计,能够实现卓越性能,从而优化业务的方方面面. ...

  2. 重构第15天 移除重复的代码(Remove Duplication)

    理解:移除重复的代码,顾名思义就是把多处重复的代码搬移到一个公共的地方,来减少代码量,提高代码可维护性. 详解:看下面的例子就很容易理解 重构前code using System; using Sys ...

  3. ASP.NET MVC的路由

    好久没写博文了,感觉最近好像少了点动力.唉!这回就看看这个MVC的路由. 说这个路由机制其实不是MVC里面特有的,ASP.NET里面本身就有的,只不过在WebForm里面一般比较少用,而在MVC里就是 ...

  4. 使用innerHTML获取HTML代码时,HTML标记属性的双引号好多都消失不见了,原来是属性值中包含空格才会保留双引号

    最近搞的一个项目中所使用的方式比较奇怪,用Label显示HTML内容,然后不断地使用JS把Label的innerHTML复制到TextBox中. 但是,昨天发现了一个问题,获取元素值的时候,有时候正常 ...

  5. 从web页面启动winform程序的实现方法

    本文实现的需求是: A.通过web页面启动winform程序: B.将页面的参数传递给winform程序: C.winform程序已经启动并正在运行时,从web页面不能重新启动winform程序,只是 ...

  6. 操作AppConfig.xml中AppSettings对应值字符串

    //查询AppSettings的key         public static List sql()         {             List list = new List();   ...

  7. java File.mkdirs和mkdir区别

    File f = new File("e://xxx//yyy"); System.out.println(f.mkdirs());//生成所有目录,一般来说,这个方法稳健性更好, ...

  8. 我所了解的WEB开发 (1)

    开始接触网站开发的时候,概念里就对静态网站和动态网站有了简单的区分,静态网站仅仅是纯粹的HTML网页,动态网站是需要采用asp 连接数据库(比如access).那个时候听说高手都是使用 Notepad ...

  9. JSP利用freemarker生成基于word模板的word文档

    利用freemarker生成基于word模板的word文档 freemarker简介 FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出.FreeMarker与Web容器 ...

  10. 响应式SharePoint模版页

    一张好的皮肤显然的会给你的项目加分不少.特别是大部分的项目,UI甚至可以决定成败. SharePoint在这方面一直都做得不好,曾经我有好多项目都是坐在美工旁边来一起修改样式.痛苦的经历. 不久以前, ...