Computer

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

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
 
 
经典的问题,第一次Dp先处理好子树的 最远 , 次远距离( 有一边[ u , v , w ] , u 的次远可能是最远路上儿子 v 的最远( u的次远 + w ) )~~
解决的时候考虑 temp 是来自父亲路径上的最长路,然后对 儿子分成 是否是最远路上的~ 来求解答案~
 
#include <bits/stdc++.h>
using namespace std ;
const int N = ; int dp[N][] , son[N][] , ans[N] , n ;
int eh[N] , et[N<<] , nxt[N<<] , ew[N<<] , tot ; void init() {
memset( eh , - , sizeof eh );
tot = ;
} void addedge( int u , int v , int w ) {
et[tot] = v ; ew[tot] = w ; nxt[tot] = eh[u] ; eh[u] = tot++ ;
et[tot] = u ; ew[tot] = w ; nxt[tot] = eh[v] ; eh[v] = tot++ ;
} int Dp( int u , int fa ) {
for( int i = eh[u] ; ~i ; i = nxt[i] ) {
int v = et[i] , w = ew[i] ;
if( v == fa ) continue ;
int tmp = Dp( v , u ) + w ;
if( tmp > dp[u][] ) {
dp[u][] = tmp ;
son[u][] = v ;
}
if( dp[u][] > dp[u][] ) {
swap( dp[u][] , dp[u][] ) ;
swap( son[u][] , son[u][]) ;
}
}
return dp[u][] ;
} void Solve( int u , int fa , int tmp ) {
ans[u] = max( dp[u][] , tmp ) ;
for( int i = eh[u] ; ~i ; i = nxt[i] ) {
int v = et[i] , w = ew[i] ;
if( v == fa ) continue ;
if( v == son[u][] ) {
Solve( v , u , max( dp[u][] , tmp ) + w ) ;
} else {
Solve( v , u , max( dp[u][] , tmp ) + w ) ;
}
}
} int main () {
while( ~scanf("%d",&n) ) {
init();
for( int i = ; i <= n ; ++i ) {
int v , w ; scanf("%d%d",&v,&w);
addedge( i , v , w ) ;
}
memset( dp , , sizeof dp ) ;
Dp( , );
//for( int i = 1 ; i <= n ; ++i ) cout << i << ' ' << dp[i][0] << ' ' << dp[i][1] << endl ;
Solve( , , );
for( int i = ; i <= n ; ++i ) printf("%d\n",ans[i]);
}
return ;
}

HDU 2196 Computer( 树上节点的最远距离 )的更多相关文章

  1. HDU 2196 Computer (树上最长路)【树形DP】

    <题目链接> 题目大意: 输出树上每个点到其它点的最大距离. 解题分析: 下面的做法是将树看成有向图的做法,计算最长路需要考虑几种情况. dp[i][0] : 表示以i为根的子树中的结点与 ...

  2. HDU 2196 Computer 树形DP经典题

    链接:http://acm.hdu.edu.cn/showproblem.php? pid=2196 题意:每一个电脑都用线连接到了还有一台电脑,连接用的线有一定的长度,最后把全部电脑连成了一棵树,问 ...

  3. HDU 2196 Computer (树dp)

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

  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(求树上每一个节点到其他点的最远距离)

    解题思路: 求出树的直径的两个端点.则树上每一个节点到其它点的最远距离一定是到这两个端点的距离中最长的那一个. #include <iostream> #include <cstri ...

  6. HDU 2196 求树上所有点能到达的最远距离

    其实我不是想做这道题的...只是今天考试考了一道类似的题...然后我挂了... 但是乱搞一下还是有80分....可惜没想到正解啊! 所以今天的考试题是: 巡访 (path.pas/c/cpp) Cha ...

  7. HDU 2196 Computer(求树上每个点的最长距离)

    题意: 这题想了挺久的, 参考了kuangbin大神的代码:https://www.cnblogs.com/kuangbin/archive/2012/08/28/2659915.html 给出树上边 ...

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

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

  9. hdu 2196 Computer 树形dp模板题

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

随机推荐

  1. vue学习-day02(自定义指令,生命周期)

    目录: 1.案例:品牌管理    2.Vue-devtools的两种安装方式    3.过滤器,自定义全局或私有过滤器    4.鼠标按键事件的修饰符    5.自定义全局指令:让文本框获取焦点   ...

  2. django操作cookie和session

    一.cookie:保存在客户端浏览器上的键值对 Cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会 ...

  3. 【技术分享:python 应用之二】解锁用 VSCode 写 python 的正确姿势

    之前一直用 notepad++ 作为编辑器,偶然发现了 VScode 便被它的颜值吸引.用过之后发现它启动快速,插件丰富,下载安装后几乎不用怎么配置就可以直接使用,而且还支持 markdown.当然, ...

  4. 向android模拟器打电话发短信的简单方法

    在开发android应用程序时,有时候需要测试一下向android手机拨打电话发送短信时该应用程序的反应.譬如编写一个广播接收器,来提示用户有短信收到或者处理短信,就需要向该手机发送短信来进行测试.这 ...

  5. HTTP深入浅出http请求(转)-----http请求的过程和实现机制

    摘要:此文章大概讲明了http请求的过程和实现机制,可以作为了解,至于请求头和响应头的具体信息需要查看下一篇随笔:Http请求详解(转)----请求+响应各字段详解   HTTP(HyperText ...

  6. Spring4配置文件模板

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  7. libusb获取usb设备的idVendor(vid),idProduct(pid),以及Serial Number

    发表于2015/6/23 21:55:11  4594人阅读 最近在做关于usb设备的项目,用到了libusb,发现关于这个的函数库的介绍,讲解很少,下面仅仅是简单展示一些基本的使用方法,以备后用. ...

  8. OperationCenter Docker运行环境及其依赖启动脚本

    1.Portainer docker rm -f portainer docker run -d -p : --name portainer --restart always portainer/po ...

  9. 如果将get请求转换成post请求

    td><a href="emp/${emp.id}">Edit</a></td> <form action="" ...

  10. Quartz安装包中的15个example

    Welcome======= Welcome to the Quartz Examples directory. This directory contains 15 examples that sh ...