HDU 2196 Computer( 树上节点的最远距离 )
Computer
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4080 Accepted Submission(s): 2043
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.
#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( 树上节点的最远距离 )的更多相关文章
- HDU 2196 Computer (树上最长路)【树形DP】
<题目链接> 题目大意: 输出树上每个点到其它点的最大距离. 解题分析: 下面的做法是将树看成有向图的做法,计算最长路需要考虑几种情况. dp[i][0] : 表示以i为根的子树中的结点与 ...
- HDU 2196 Computer 树形DP经典题
链接:http://acm.hdu.edu.cn/showproblem.php? pid=2196 题意:每一个电脑都用线连接到了还有一台电脑,连接用的线有一定的长度,最后把全部电脑连成了一棵树,问 ...
- HDU 2196 Computer (树dp)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2196 给你n个点,n-1条边,然后给你每条边的权值.输出每个点能对应其他点的最远距离是多少 ...
- hdu 2196 computer
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 2196 Computer(求树上每一个节点到其他点的最远距离)
解题思路: 求出树的直径的两个端点.则树上每一个节点到其它点的最远距离一定是到这两个端点的距离中最长的那一个. #include <iostream> #include <cstri ...
- HDU 2196 求树上所有点能到达的最远距离
其实我不是想做这道题的...只是今天考试考了一道类似的题...然后我挂了... 但是乱搞一下还是有80分....可惜没想到正解啊! 所以今天的考试题是: 巡访 (path.pas/c/cpp) Cha ...
- HDU 2196 Computer(求树上每个点的最长距离)
题意: 这题想了挺久的, 参考了kuangbin大神的代码:https://www.cnblogs.com/kuangbin/archive/2012/08/28/2659915.html 给出树上边 ...
- 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模板题
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
随机推荐
- Linux入门培训教程 linux网络编程socket介绍
一.概念介绍 网络程序分为服务端程序和客户端程序.服务端即提供服务的一方,客户端为请求服务的一方.但实际情况是有些程序的客户端.服务器端角色不是这么明显,即互为Linux培训 客户端和服务端. 我们编 ...
- Vue项目开发,nprogress进度条加载之超详细讲解及实战案例
Nprogress的默认进度条很细,它的设计灵感主要来源于 谷歌,YouTube 他的安装方式也很简单,你可以有两种使用方式: 直接引入js和css文件 使用npm安装的的方式 直接引入: Npm安装 ...
- MySQL_DML操作
DML(Data Manipulation Laguage)指对数据库数据的增(Create)删(Delete)改(Update)操作 1.增加操作 (1)先创建一个表,如图所示: 语法:Insert ...
- Selenium 控制浏览器
webdriver提供了操作浏览器的一些基本方法,例如:打开,前进,后退,刷新,设置窗口大小,截屏,退出等 一.打开网页 代码: # coding = utf-8 from time import s ...
- (53)LINUX应用编程和网络编程之八Linux网络基础
3.8.1.网络通信概述 3.8.1.1.从进程间通信说起:网络域套接字socket,网络通信其实就是位于网络中不同主机上面的2个进程之间的通信. 3.8.1.2.网络通信的层次 (1)硬件部分:网卡 ...
- 配置Nginx和Apache允许指定域名CORS跨域访问
前后端分离开发,导致前端项目需要跨域请求后端接口,解决方法有很多,本文只介绍两个: 1. 修改后端程序代码实现允许跨域请求 2. 修改服务器配置文件实现允许跨域请求 正文: 方法1:修改后端程序代码实 ...
- springboot+mybatis+SpringSecurity 实现用户角色数据库管理(一)
本文使用springboot+mybatis+SpringSecurity 实现用户权限数据库管理 实现用户和角色用数据库存储,而资源(url)和权限的对应采用硬编码配置. 也就是角色可以访问的权限通 ...
- 九、设置RF自定义的日志输出路径
在Arguments输入-d E:\\robot,每次运行完都会发送该目录日志
- docker常用软件安装及使用
linux安装docker: sudo wget -qO- https://get.docker.com | sh 启动docker: service docker start 搜索镜像: docke ...
- .Net 逆向 Reflector之reflexil使用
网上下载了一款商用的教育培训类软件,是用.Net写的,标榜的是免费的,但是只能试用一个月,商家很精明,用此方法推广招揽客户,但是公司在这一块却没有预算购买,一开始就想着既然是商用软件,安全机制做的肯定 ...