题解报告:hdu 2196 Computer(树形dp)
Problem Description
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
Output
Sample Input
Sample Output
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
struct node{int to,next,len;}edge[maxn<<];
int n,x,y,cnt,head[maxn],dp[maxn][],lgst[maxn];
void add_edge(int u,int v,int w){
edge[cnt].to=v;
edge[cnt].len=w;
edge[cnt].next=head[u];
head[u]=cnt++;
}
int dfs1(int u,int fa){
int Dmax=,Dsec=;
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].to;
if(v^fa){
int nowd=dfs1(v,u)+edge[i].len;
if(nowd>Dmax)lgst[u]=v,Dsec=Dmax,Dmax=nowd;//lgst[u]=v记录u的正向最长路径上的节点v
else if(nowd>Dsec)Dsec=nowd;
}
}
dp[u][]=Dmax,dp[u][]=Dsec;//记录每个节点的正向最长距离和正向次长距离
return Dmax;//返回正向最长距离
}
void dfs2(int u,int fa){
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].to;
if(v^fa){
if(v==lgst[u])dp[v][]=max(dp[u][],dp[u][])+edge[i].len;
else dp[v][]=max(dp[u][],dp[u][])+edge[i].len;
dfs2(v,u);
}
}
}
int main(){
while(~scanf("%d",&n)){
cnt=;memset(head,-,sizeof(head));
memset(dp,,sizeof(dp));
memset(lgst,,sizeof(lgst));
for(int i=;i<=n;++i){
scanf("%d%d",&x,&y);
add_edge(i,x,y);
add_edge(x,i,y);
}
dfs1(,-);
dfs2(,-);
for(int i=;i<=n;++i)
printf("%d\n",max(dp[i][],dp[i][]));
}
return ;
}
题解报告:hdu 2196 Computer(树形dp)的更多相关文章
- 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经典题
链接:http://acm.hdu.edu.cn/showproblem.php? pid=2196 题意:每一个电脑都用线连接到了还有一台电脑,连接用的线有一定的长度,最后把全部电脑连成了一棵树,问 ...
- HDU 2196 Computer 树形DP 经典题
给出一棵树,边有权值,求出离每一个节点最远的点的距离 树形DP,经典题 本来这道题是无根树,可以随意选择root, 但是根据输入数据的方式,选择root=1明显可以方便很多. 我们先把边权转化为点权, ...
- 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 ...
- 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)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2196 给你n个点,n-1条边,然后给你每条边的权值.输出每个点能对应其他点的最远距离是多少 ...
- HDU - 2196(树形DP)
题目: A school bought the first computer some time ago(so this computer's id is 1). During the recent ...
- hdu 2196【树形dp】
http://acm.hdu.edu.cn/showproblem.php?pid=2196 题意:找出树中每个节点到其它点的最远距离. 题解: 首先这是一棵树,对于节点v来说,它到达其它点的最远距离 ...
- HDU 2196 Compute --树形dp
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
随机推荐
- Spark 学习笔记:(二)编程指引(Scala版)
参考: http://spark.apache.org/docs/latest/programming-guide.html 后面懒得翻译了,英文记的,以后复习时再翻. 摘要:每个Spark appl ...
- python day-15 匿名函数 sorted ()函数 filter()函数 map()函数 递归 二分法
一.匿名函数 匿名函数的结构:变量 = lamda 参数: 返回值 a = lamda x : x*x # x为参数, : 后边的为函数体 print(a(x)) def ...
- BootStrap-DualListBox怎样改造成为双树
BootStrap-DualListBox能够实现将所选择的列表项显示到右边,未选的列表项显示到左边. 但是左右两边的下拉框中都是单级列表.如果要实现将两边都是树(缩进树),选择某个节点时,其子节点也 ...
- 通过反射调用一个单列的方法(单列必须有“getInstance”方法)
Class<?> _clazz = Class.forName(_clazzName); if (_clazz != null) { Method _getInstance = _claz ...
- debian iptables持久化
1 保存iptables iptables-save > /etc/iptables.rules 2 创建启动文件 touch /etc/network/if-pre-up.d/iptabl ...
- Remove FileUtil#copyMerge
[HADOOP-12967] Remove FileUtil#copyMerge - ASF JIRA https://issues.apache.org/jira/browse/HADOOP-129 ...
- POJ2728 Desert King —— 最优比率生成树 二分法
题目链接:http://poj.org/problem?id=2728 Desert King Time Limit: 3000MS Memory Limit: 65536K Total Subm ...
- golang defer使用——资源关闭时候多用
defer Go语言中有种不错的设计,即延迟(defer)语句,你可以在函数中添加多个defer语句.当函数执行到最后时,这些defer语句会按照逆序执行,最后该函数返回.特别是当你在进行一些打开资源 ...
- [Selenium] 操作 警告框、提示框、确认框
以如下页面为例: http://sislands.com/coin70/week1/dialogbox.htm 示例代码: pachage com.learningselenium.normalweb ...
- [Selenium] The most commonly used CSSSelector
CSSSelector Example Description element.element div.dropdown Select all <div> elements whose ...