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 ...
随机推荐
- Neo4j 两种索引Legacy Index与Schema Index区别
Legacy Indexes 在Neo4j 2.0版本之前,Legacy index被称作indexes.这个索引是通过外部图存储在外的Lucene实现,允许“节点”和“联系”以key:value键值 ...
- 使用JavaScript设置、获取父子页面中的值
一:获取父页面中的值 有二种方法windows.open()和windows.showModalDialog() 1.windos.open(URL,name,reatures,replace) 再父 ...
- 设置Android studio内容的主题
下载主题Jar包 http://color-themes.com/?view=theme&id=563a1a6e80b4acf11273ae76 导入主题: File->Import s ...
- 初学Html
HTML,超文本标记语言,文本解释性语言,它的源代码不通过编译而直接在浏览器中运行时被翻译,其决定的是网页的结构和内容. 一谈html,自然首先是浏览器,比较具有代表性的浏览器则是谷歌.火狐.IE.苹 ...
- Oracle创建、删除表空间、用户
1.创建临时表空间 create temporary tablespace linshi tempfile 'e:\linshi.dbf' size 50m autoextend on next 50 ...
- open Live Writer配置步骤
一.关于 Open Live Writer 微软推出的一款能够免费使用的博客写作软件,主要为用户提供博客在线撰写和编辑功能,可以离线编辑,联网时同步到各大博客网站上去. 接下来,介绍如何使用这款工具发 ...
- 使用MVC和EF,在保存数据的时候报错:System.Data.Entity.Validation.DbEntityValidationException: 对一个或多个实体的验证失败。有关详细信息,请参阅“EntityValidationErrors”属性。
public UserInfo Create(UserInfo entity) { try { context.UserInfoes.Add(entity); context.SaveChanges( ...
- 【转】浏览器内核、渲染引擎、js引擎
[1]定义 浏览器内核分成两部分渲染引擎和js引擎,由于js引擎越来越独立,内核就倾向于只指渲染引擎 渲染引擎是一种对HTML文档进行解析并将其显示在页面上的工具[2]常见引擎 渲染引擎: firef ...
- 基于Python的接口测试框架
分析 接口是基于HTTP协议的,那么说白了,就是发起HTTP请求就行了,对于Python来说比较简单.直接使用requests就可以很轻松的完成任务. 架构 整个框架是比较小的,涉及的东西也比较少,只 ...
- elasticsearch【更新】操作
基于上一篇博文基础上,进行es的操作,document的新增比较简单,就不说了,这里主要说说更新操作. 更新操作,有两大类,一个是Replace,一个是Update,就是说一个是替换,一个是更新. 替 ...