hdu 2196 computer
Computer
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6225 Accepted Submission(s):
3142
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.
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.
number Si for i-th computer (1<=i<=N).
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 10005
using namespace std;
struct X
{
int v,q,f,n;
}x[N];
int n,m,f[N][];//f[i][0]表示从子树最大,f[i][1]表示次大,f[i][2]表示从父节点最大
void dfs1(int u)
{
for(int i=x[u].f;i;i=x[i].n)
{
dfs1(x[i].v);
if(f[x[i].v][]+x[i].q>f[u][])
{
f[u][]=f[u][];
f[u][]=f[x[i].v][]+x[i].q;
}
else if(f[u][]<f[x[i].v][]+x[i].q) f[u][]=f[x[i].v][]+x[i].q;
}
}
void dfs2(int u)
{
for(int i=x[u].f;i;i=x[i].n)
{
f[x[i].v][]=x[i].q+max(f[u][],f[x[i].v][]+x[i].q==f[u][]?f[u][]:f[u][]);
dfs2(x[i].v);
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(x,,sizeof(x));
memset(f,,sizeof(f));
for(int i=;i<n;i++)
{
int u;
scanf("%d%d",&u,&x[i].q);
x[i].v=i+;
x[i].n=x[u].f;
x[u].f=i;
}
dfs1();
dfs2();
for(int i=;i<=n;i++) printf("%d\n",max(f[i][],f[i][]));//两种比较取最大
}
return ;
}
hdu 2196 computer的更多相关文章
- HDU 2196 Computer (树dp)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2196 给你n个点,n-1条边,然后给你每条边的权值.输出每个点能对应其他点的最远距离是多少 ...
- 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模板题
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 树的直径
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- 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)
Problem Description A school bought the first computer some time ago(so this computer's id is 1). Du ...
- HDU 2196 Computer( 树上节点的最远距离 )
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
随机推荐
- Java实验一
20145113 Java实验一 使用JDK编译.运行简单的Java程序 安装JDK并配置相关环境 安装JDK后配置环境变量 计算机→属性→高级系统设置→高级→环境变量 新建 JAVA_HOME 变量 ...
- 在ubuntu下安装QQ
(参考链接 :http://jingyan.baidu.com/album/47a29f24577776c01423991a.html?picindex=3) 一 .安装 wine 1.下载一个 ...
- Android Listview 性能优化
首先我一般使用的适配器是BaseAdapter,其中有两个方法最主要,分别是: getCount,getView, 在对Listview 进行优化的时候,首先使用 convertview 和viewH ...
- RabbitMQ中 exchange、route、queue的关系
从AMQP协议可以看出,MessageQueue.Exchange和Binding构成了AMQP协议的核心,下面我们就围绕这三个主要组件 从应用使用的角度全面的介绍如何利用Rabbit MQ构建 ...
- SSIS 目录
微软 BI 系列随笔 - SSIS 2012 基础 - SSIS 目录 上一篇讲解了使用SSIS参数与环境,由于涉及到了SSIS目录的相关知识和概念,本篇将对其进行讲解. 注:在之前的版本中,是使用整 ...
- 如何成为一名合格甚至优秀的个人草根站长(转载自ChinaZ)
这章本来不想写来的,后来琢磨琢磨还是废话一下吧.主要是想说下现在草根站长的状态和如何成为一名合格的甚至优秀的草站站长. 伟大的草根站长们,在某些媒体的超级忽悠下全来到网络上淘金来了,有在校的大学生,有 ...
- angular+bootstrap+MVC 之三,分页控件初级版
今天实现一个分页控件,效果如下: 1.HTML: <!doctype html> <!--suppress ALL --> <html ng-app="appT ...
- ssl_error_rx_record_too_long
Linux下安装SSL 当使用https访问时出现: SSL 接收到一个超出最大准许长度的记录. (错误码: ssl_error_rx_record_too_long) 网上说修改虚拟机 vi /et ...
- android学习笔记54——ContentProvider
ContentProvider ContentProvider用于实现数据共享. ContentProvider是不同应用程序之间进行数据交换的标准API,其以某种Uri的形式对外提供数据,允许其他应 ...
- ASP.NET MVC路由配置(转载自http://www.cnblogs.com/zeusro/p/RouteConfig.html )
把apress.pro.asp.net.mvc.4.framework里的CHAPTER 13翻译过来罢了. XD 首先说URL的构造. 其实这个也谈不上构造,只是语法特性吧. 命名参数规范+匿名对象 ...