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 ...
随机推荐
- noip2013 火柴排序
涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度.现在将每盒中的火柴各自排成一列,同一列火柴的高度互不相同,两列火柴之间的距离定义为: ,其中 ai 表示第一列火柴中第 i 个火柴的高度,b ...
- (转)Let’s make a DQN 系列
Let's make a DQN 系列 Let's make a DQN: Theory September 27, 2016DQN This article is part of series Le ...
- .net framework 4.0 从 GAC 卸载 程序集
.net framework 4.0 的 GAC 目录: C:\Windows\Microsoft.NET\assembly\GAC_MSIL 要卸载,仍然使用 gacutil 命令,不要带扩展名: ...
- 淘宝ip库接口调用
function ip($ip) { $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip; $ipi ...
- C++模板编程里的主版本模板类、全特化、偏特化(C++ Type Traits)
1. 主版本模板类 首先我们来看一段初学者都能看懂,应用了模板的程序: 1 #include <iostream> 2 using namespace std; 3 4 template ...
- 操作配置文件Properties
// */ // ]]> 操作配置文件Properties Table of Contents 1 定义 2 读取配置值 3 修改和保存配置 4 注意 1 定义 csharp中在Settin ...
- java 线程的优先级
//线程的优先级 //线程1 class xc1 implements Runnable{ public void run(){ for(int i=0;i<20;i++){ System.ou ...
- java 线程的使用
java 线程的使用 //线程的使用 //需要记三个单词 //1.Thread 线程的类名 //2. Runnable 线程的接口 //3. start 执行线程 //使用继承线程类的方式实现线程 c ...
- SESSION和COOKIE的作用和区别,SESSION信息的存储方式,如何进行遍历?
二者的定义:当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cookie 会帮你在网站上所打的文字或是一些选择,都纪录下来.当下次你再光临同一个网站,WEB 服务器会先看看有没有 ...
- 鸟哥的linux私房菜学习
cat /etc/shells 系统拥有的shellcat /etc/passwd 记录用户使用的shell按两次 tab 键可显示所有可执行的指令alias 查看所有命令的别名alias lm='l ...