Computer

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2764    Accepted Submission(s): 1415

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
 
思路:先选定1为树根,进行第一次深搜,很容易求出节点u到其子节点的最长距离和次长距离,求次长距离的目的是如果u的跟节点最长路径经过u则dp的时候就不能取其跟节点的最长距离,应该取其次长距离;然后进行第二次深搜,搜索节点u经过其跟节点的最长距离。
如果令dp[u][0],dp[u][1],分别为节点u到子节点的最长,次长距离,dp[u][2]表示节点u经过其跟节点v的最长距离,则状态方程为 dp[u][2] = max(dp[v][2],dp[u][0] + edge[j].w == dp[v][0] ? dp[v][1] : dp[v][0]) + edge[j].w;
最后输出答案为max(dp[u][0],dp[u][2]),即某点u的最长距离为其到子节点的最长距离和经过其跟节点的最长距离二者之中的最大者。
 
 #include<iostream>
#include<cstdio>
#include<cstring>
#define MAX 11111
using namespace std;
typedef long long int LL;
typedef struct{
int to, next, w;
}Node;
Node edge[MAX];
LL head[MAX], dp[MAX][];
void AddEdge(int u, int v, int w, int i){
edge[i].to = v;
edge[i].w = w;
edge[i].next = head[u];
head[u] = i;
}
void dfs_to_son(int i){
LL bigest = , biger = ;
for(int j = head[i];j != -;j = edge[j].next){
int v = edge[j].to;
dfs_to_son(v);
LL temp = dp[v][] + edge[j].w;
if(bigest <= temp){
biger = bigest;
bigest = temp;
}else if(temp > biger) biger = temp;
}
dp[i][] = bigest;
dp[i][] = biger;
}
void dfs_to_father(int i){
for(int j = head[i];j != -;j = edge[j].next){
int v = edge[j].to;
dp[v][] = max(dp[i][], dp[v][] + edge[j].w == dp[i][] ? dp[i][]:dp[i][]) + edge[j].w;
dfs_to_father(v);
}
}
int main(){
int n, u, w;
/* freopen("in.c", "r", stdin); */
while(~scanf("%d", &n)){
memset(dp, , sizeof(dp));
memset(head, -, sizeof(head));
for(int i = ;i <= n;i ++){
scanf("%d%d", &u, &w);
AddEdge(u, i, w, i-);
}
dfs_to_son();
dfs_to_father();
for(int i = ;i <= n;i ++) printf("%lld\n", max(dp[i][], dp[i][]));
}
return ;
}

HDOJ --- 2196 Computer的更多相关文章

  1. hdoj 2196 Computer【树的直径求所有的以任意节点为起点的一个最长路径】

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  2. HDOJ 2196 Computer 树的直径

    由树的直径定义可得,树上随意一点到树的直径上的两个端点之中的一个的距离是最长的... 三遍BFS求树的直径并预处理距离....... Computer Time Limit: 1000/1000 MS ...

  3. HDU 2196.Computer 树形dp 树的直径

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  4. hdu 2196 computer

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  5. hdu 2196 Computer 树形dp模板题

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  6. hdu 2196 Computer(树形DP)

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  7. hdu 2196 Computer 树的直径

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem ...

  8. HDU 2196 Computer (树dp)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2196 给你n个点,n-1条边,然后给你每条边的权值.输出每个点能对应其他点的最远距离是多少 ...

  9. hdu 2196 Computer(树形DP经典)

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

随机推荐

  1. html-----007

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. Windows下ANSI、Unicode、UTF8字符编码转换

    主意:输入字符串必须是以'\0'结尾,如果输入字符串没有以'\0'结尾,请手动设置,否则转换会有错误. unsigned int EncodeUtil::AnsiToUcs2( char* pAnsi ...

  3. 【ADO.NET】3、从TXT中导入数据到数据库

    数据库字段与类型id int,Name nvarchar(20),Age int TXT文本内容为 小高-20张三-18李四-19 private void btnInput_Click(object ...

  4. ECMA中关于if与else的关系的一句英文,感觉比较经典

    Each else for which the choice of assocated if is ambiguous shall be associated with the nearest pos ...

  5. MongoDB笔记(三)启动命令mongod的参数

    上一节有关访问权限的笔记,是由启动命令mongod的参数auth引发的有关问题,这节就来看看mongod的其他参数 MongoDB启动命令mongod参数说明: 基本配置 --quiet # 安静输出 ...

  6. JS将时间戳转换为JS Date类型

    /*将JSON Date 格式转换为JavaScript 的Date 类型JSON Date 格式:"/Date(146471041000)/"*/function JSONDat ...

  7. centos 交换分区

    内容来自:http://www.huzs.net/?p=1683 一般在桌面型的用不到,因为现在内存都比较大,服务器就不一样了,但是建议无论是在桌面还是服务器上,都设置 swap 以下操作都是在 ro ...

  8. CentOS系统安全配置

    http://down.51cto.com/data/318797 http://www.centos.bz/2011/07/centos-system-security-configure/ htt ...

  9. C语言和C++中动态申请内存

      在C语言和C++的动态内存的使用方法是不同的,在C语言中要使用动态内存要包含一个头文件即 #include<malloc.h> 或者是#include<stdlib.h>  ...

  10. 【javascript 动态添加数据到 HTML 页面】

    今天简单的学习了一下有关对象字面量的定义和 javascript 如何取出对象字面量的值的知识,javascript 动态添加数据到 HTML 页面的问题. [学习目标]有如下的一组数据通过 Ajax ...