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 ...
随机推荐
- winsock教程- windows下的socket编程(c语言实现)
winsock教程- windows下的socket编程(c语言实现) 使用winsock进行socket 编程 这是一个学习windows下socket编程(c语言)的快速指南.这是因为一下 ...
- 【Unity3D基础教程】给初学者看的Unity教程(一):GameObject,Compoent,Time,Input,Physics
作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点推荐.谢谢! Unity3D重要模块的类图 最近刚刚完成了一 ...
- 030. asp.net中DataList数据绑定跳转(两种方式)的完整示例
前台代码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.as ...
- MySQL性能优化总结(转)https://yq.aliyun.com/articles/24249
摘要: 一.MySQL的主要适用场景 1.Web网站系统 2.日志记录系统 3.数据仓库系统 4.嵌入式系统 二.MySQL架构图: 三.MySQL存储引擎概述 1)MyISAM存储引擎 MyIS ...
- LeetCode "Binary Tree Level Order Traversal II" using DFS
BFS solution is intuitive - here I will show a DFS based solution: /** * Definition for a binary tre ...
- jquery跨域请求jsonp
服务端PHP代码 header('Content-Type:application/json; charset=utf-8'); $arr = array('a'=>1, 'b'=>2, ...
- python学习-day15:函数作用域、匿名函数、函数式编程、map、filter、reduce函数、内置函数r
---恢复内容开始--- 一.全局变量与局部变量 在子程序中定义的变量称为局部变量, 在程序的一开始定义的变量称为全局变量. 全局变量作用域是整个程序,局部变量作用域是定义该变量的子程序.当全局变量与 ...
- Hibernate5.2之HQL查询
Hibernate5.2之HQL查询 一. 介绍 Hibernate的 ...
- 鸟哥的linux私房菜学习
cat /etc/shells 系统拥有的shellcat /etc/passwd 记录用户使用的shell按两次 tab 键可显示所有可执行的指令alias 查看所有命令的别名alias lm='l ...
- el 中requestScope和param
${scope.attribute},其中scope指pageSocpe.requestScope.sessionScope.applicationScope,attribute指的就是你在某个sco ...