hdu 2196 Computer(树形DP)
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)的更多相关文章
- HDU 2196.Computer 树形dp 树的直径
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 2196 Computer 树形DP经典题
链接:http://acm.hdu.edu.cn/showproblem.php? pid=2196 题意:每一个电脑都用线连接到了还有一台电脑,连接用的线有一定的长度,最后把全部电脑连成了一棵树,问 ...
- HDU 2196 Computer 树形DP 经典题
给出一棵树,边有权值,求出离每一个节点最远的点的距离 树形DP,经典题 本来这道题是无根树,可以随意选择root, 但是根据输入数据的方式,选择root=1明显可以方便很多. 我们先把边权转化为点权, ...
- hdu 2196 Computer 树形dp模板题
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- hdu 2196 Computer(树形DP经典)
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 2196 Computer (树dp)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2196 给你n个点,n-1条边,然后给你每条边的权值.输出每个点能对应其他点的最远距离是多少 ...
- HDU - 2196(树形DP)
题目: A school bought the first computer some time ago(so this computer's id is 1). During the recent ...
- hdu 2196【树形dp】
http://acm.hdu.edu.cn/showproblem.php?pid=2196 题意:找出树中每个节点到其它点的最远距离. 题解: 首先这是一棵树,对于节点v来说,它到达其它点的最远距离 ...
- HDU 2196 Compute --树形dp
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
随机推荐
- C#实用杂记-EF全性能优化技巧
原文链接:http://www.makmong.com/947.html#comment-31 EntityFramework 优化建议 2016年1月15日 下午4:54 LEILINKANG ...
- 全球2/3的DNS瘫痪 顶级域名根服务器故障
1月21日下午消息,据多家DNS服务商透露,今日下午3点,全国所有通用顶级域的根出现异常,导致部分国内用户无法访问.com域名网站,对全国互联网链接造成系统性影响. 根服务器主要用来管理互联网的主 ...
- 几种web字体格式
目前,文字信息仍是网站最主要的内容,随着CSS3技术的不断成熟,Web字体逐渐成为话题,这项让未来Web更加丰富多彩的技术拥有多种实现方案,其中之一是通过@font-face属性在网页中嵌入自定义字体 ...
- [CLR via C#]12. 泛型
泛型(generic)是CLR和编程语言提供一种特殊机制,它支持另一种形式的代码重用,即"算法重用". 简单地说,开发人员先定义好一个算法,比如排序.搜索.交换等.但是定义算法的开 ...
- 狂屌的Windows下的定时任务工具xStarter
xStarter是一款将某些常规计算机操作自动化进行为目的的程序. 它不能为你生成word文件,但是它可以周期性地为你备份文件以保持完整性. 程序的特点有:加强的任务计划工具;在系统事件上执行任务;用 ...
- DirectShow程序运行过程简析
这段时间一直在学习陆其明老师的<DirectShow开发指南>一书,书中对DirectShow的很多细节讲解清晰,但是却容易让人缺少对全局的把握.在学习过程中,整理了关于DirectSho ...
- ADO.NET 实体类和数据访问类
SQL数据库字符串注入攻击:需要使用cmd.Parameters这个集合占位符: @key 代表这个位置用这个占位符占住了 Parameters这个集合中将此占位符所代表的数据补全 cmd.Param ...
- [Xamarin.Android] 使用Component套件
[Xamarin.Android] 使用Component套件 前言 在Xamarin中,可以将自己开发的项目包装成为Component套件发布至Xamarin Component Store,来提供 ...
- Flexbox实现垂直水平居中
Flexbox(伸缩盒)是CSS3中新增的特性,利用这个属性可以解决页面中的居中问题.只需要3行代码就可以实现,不需要设置元素的尺寸,能够自适应页面. 这个方法只能在现代浏览器上有效,IE10+.ch ...
- cl_gui_cfw=>dispatch
将已经触发的EVENT发送给他们各自的EVENT HANDLER,以便让这些事件得到响应. 根据返回值可以判断是否发送成功. CALL METHOD cl_gui_cfw=>dispatch ...