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. App.Config详解

    App.Config详解 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序.配置文件的根节点是c ...

  2. Ext.GridPanel 用法总结(一)—— Grid基本用法

    Ext.GridPanel 用法总结(一)—— Grid基本用法 摘自:http://www.cnblogs.com/luluping/archive/2009/08/01/1536645.html ...

  3. DotNetCore跨平台~Startup类的介绍

    新宠儿 DotNetCore是.net5.0版本,之所以不叫.net5.0为的就是不让我们把它与前面的.net混为一淡,它将是真正意义的跨平台开发语言,在网上也有相关介绍,中国的一些大牛也发了相关文章 ...

  4. 【jQuery基础学习】01 jQuery选择器

    关于CSS选择器 jQuery选择器涉及到CSS,CSS技术使得网页的结构与表现样式完全分离. 同样CSS也需要找到某个网页的结构才能改变其样式,这就是CSS选择器. 常用的CSS选择器如下: 标签选 ...

  5. KMP的原理详细讲解

    1.kmp算法的原理: 本部分内容转自:http://www.cnblogs.com/c-cloud/p/3224788.html及                           http:// ...

  6. 设置php下载文件的超时时间

    使用curl 可以使用curl自己实现一个curl_file_get_contents函数 //CURLOPT_FOLLOWLOCATION TRUE 时将会根据服务器返回 HTTP 头中的 &quo ...

  7. Natural language style method declaration and usages in programming languages

    More descriptive way to declare and use a method in programming languages At present, in most progra ...

  8. jDiameter介绍以及使用

    jDiameter是Diameter协议的开源实现(比较不幸的是AGPL 3.0协议),项目地址https://github.com/RestComm/jdiameter. 项目框架 jDiamete ...

  9. [Design Pattern] Substitute Interface

    [Design Pattern] Substitute Interface 目的 将对象的成员建立为替身接口的成员,用来解耦对象之间的循环相依. 情景 假设开发人员接手一个系统,在系统里有订单对象.送 ...

  10. 奇怪的float

    我在项目的实践中遇到了这样的一个问题 <div class="main"> <p>aaaa</p> <p>bbbb</p> ...