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. 从1G到5G发展史(3GPP是个什么组织 为啥5G标准离不开它)

    1.“3GPP”组织建立的来龙去脉 3GPP一直以来在人们心中是一个神秘的组织,很多用户对于它的理解和认知,说不清,道不明.最近关于5G网络的诸多报道,都陈述了“5G网络”的标准是由“3GPP”来规定 ...

  2. windows 环境如何启动 redis ?

    1.cd 到 redis 的安装目录 C:\Users\dell>cd C:\redis 2.执行 redis 启动命令 C:\redis>redis-server.exe redis.w ...

  3. tomcat8 的优化

    1.下载tomcat8 2.配置 修改tomcat_user.xml,配置管理用户(设置角色,和用户密码) <role rolename="manager"/> < ...

  4. JAVA语言动手动脑问题

    1. 早期经常这样定义变量  int value=100;前面的示例中这样定义变量  MyClass obj = new MyClass(); 这两种方式定义的变量是一样的吗?   答:不一样,后者开 ...

  5. spring cloud:config-eureka-refresh

    config-server-eureka project 1. File-->new spring project 2.add dependency <parent> <gro ...

  6. LinkedList Stack

  7. MVC的各个部分都有那些技术来实现?如何实现?

    MVC是Model-View-Controller的简写. Model 代表的是应用的业务逻辑(通过JavaBean,EJB组件实现), View 是应用的表示面(由JSP页面产生), Control ...

  8. saml2协议sp-initial登录过程

    登录过程如下所示: 一次完整的saml认证过程,包括一次samlrequest和samlresponse, 首先用户如果想访问一个sp,sp会先检验用户是否登录,如果用户已经登录,即可以正常访问sp的 ...

  9. Linux_LEMP

    目录 目录 LEMP Nginx mysql PHP php-fpm Script LEMP Nginx是一个高性能的HTTP和反向代理服务器,也是一个 IMAP/POP3/SMTP 服务器,因它的稳 ...

  10. sql语句实现行转列的3种方法实例

    sql语句实现行转列的3种方法实例 一般在做数据统计的时候会用到行转列,假如要统计学生的成绩,数据库里查询出来的会是这样的,但这并不能达到想要的效果,所以要在查询的时候做一下处理,下面话不多说了,来一 ...