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. Java设计模式:23种设计模式全面解析(超级详细)

    设计模式(Design Pattern)是前辈们对代码开发经验的总结,是解决特定问题的一系列套路.它不是语法规定,而是一套用来提高代码可复用性.可维护性.可读性.稳健性以及安全性的解决方案. 1995 ...

  2. Luogu P3408 恋爱

    题目链接:Click here Solution: 设f[x]表示要使x向它的父亲写信需要花的最少的钱,per[x]为要使x向它的父亲写信最少要多少人 则\(f[x]=\sum_{i=1}^{per[ ...

  3. pyinstaller打包的exe太大?你需要嵌入式python玄学 探索篇

    上篇我们讲到pip的安装以及普通库用pip的安装方法 CodingDog:pyinstaller打包的exe太大?你需要嵌入式python玄学 拓展篇​zhuanlan.zhihu.com 问题纷沓而 ...

  4. navicat_premium_x64最新版安装说明

    先到官网下载最新的navicat http://www.navicat.com.cn/ 下载破解文件 链接: https://pan.baidu.com/s/1hhsh5Tfe4c_lQeyX8D-C ...

  5. 图的普里姆(Prim)算法求最小生成树

    关于图的最小生成树算法------普里姆算法 首先我们先初始化一张图: 设置两个数据结构来分别代表我们需要存储的数据: lowcost[i]:表示以i为终点的边的最小权值,当lowcost[i]=0说 ...

  6. html readonly 和 disable 区别

    readonly 和 disable的区别Readonly和Disabled它们都能够做到使用户不能够更改表单域中的内容.但是它们之间有着微小的差别,总结如下: Readonly只针对input(te ...

  7. Vue知识整理9:class与style绑定

    1.v-bind:class:绑定class样式.通过控制isActive变量值来实现是否显示:通过.active样式设置背景颜色. 2.支持普通的class与v-bind绑定样式混合使用: v-bi ...

  8. 阶段3 1.Mybatis_01.Mybatis课程介绍及环境搭建_02.三层架构和ssm框架的对应关系

  9. Delphi中TQuery.Filter用法

    今天维护一个老项目是用delphi5 + BDE写的.为了更方便查询数据,就增加一个查询功能.由于数据量查询出来后就比较少,于是就想到Filter like 但 BDE并不支持 Filter = 'n ...

  10. .NetCore2.0项目之ABP+Vue(IView框架)单页应用之路,启动

    首先很久没有好好静下心来做点东西了,一直用忙碌做借口,实际还是懒,今天决定动一动. 第一步,下载自己的项目模板 首先vue项目国内的暂时还没有,要登录https://aspnetboilerplate ...