题意:

给出电脑网络连接树,求每个节点的为起点的最长距离

分析:

这道题开始状态想不出来,放了一段时间,后来注意到例题上有这道题,每个节点的最长距离可由父节点的最长距离,次长距离,和子节点的最长距离(三者取最大)决定。先用一个dfs求出各节点由各子树确定的最长距离,次长距离,再用一个dfs由父节点推各子节点的最长距离。

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
#define N 10010
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = 1000000007;
struct edge{
int t,d;
};
//p[i]由父节点来的最长距离
//dp[i]由子树来的最长距离
//g[i]由子树来的次长距离
int dp[N],p[N],g[N],n,longest[N];
vector<edge>e[N];
int dfs(int root){
if(e[root].size()==0)
return 0;
if(dp[root])return dp[root];//记忆化搜索
int tmp;
for(int i=0;i<e[root].size();++i){
int son=e[root][i].t;
int cost=e[root][i].d;
if(dfs(son)+cost>dp[root]){
g[root]=dp[root];
dp[root]=dp[son]+cost;
tmp=son;
}
else if(dp[son]+cost>g[root])g[root]=dp[son]+cost;
}
longest[root]=tmp;//把取最长距离的子节点存起来,方便后面由次长距离的更新最长
return dp[root];
}
void dfs1(int root){
for(int i=0;i<e[root].size();++i){
int son=e[root][i].t;
int cost=e[root][i].d;
if(son==longest[root])//若在该子节点取得最长距离 ,由父节点的来自父节点的最长距离和来自子树的次长距离更新
p[son]=max(p[root],g[root])+cost;
else
p[son]=max(p[root],dp[root])+cost;//否则,由父节点的来自父节点的最长距离和来自子树的最长距离更新
dfs1(son);
}
}
int main()
{
while(~scanf("%d",&n)){
edge b;
int a;
memset(dp,0,sizeof(dp));
memset(p,0,sizeof(p));
memset(g,0,sizeof(g));
for(int i=1;i<=n;++i){
e[i].clear();
}
for(int i=2;i<=n;++i){
scanf("%d%d",&a,&b.d);
b.t=i;
e[a].push_back(b);
}
dfs(1);
dfs1(1);
for(int i=1;i<=n;++i){
printf("%d\n",max(p[i],dp[i]));
}
}
return 0;
}

  

HDU 2196-Computer(树形dp)的更多相关文章

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

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

  2. HDU 2196 Computer 树形DP经典题

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

  3. HDU 2196 Computer 树形DP 经典题

    给出一棵树,边有权值,求出离每一个节点最远的点的距离 树形DP,经典题 本来这道题是无根树,可以随意选择root, 但是根据输入数据的方式,选择root=1明显可以方便很多. 我们先把边权转化为点权, ...

  4. hdu 2196 Computer(树形DP)

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

  5. hdu 2196 Computer 树形dp模板题

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

  6. hdu 2196 Computer(树形DP经典)

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

  7. HDU 2196 Computer (树dp)

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

  8. HDU - 2196(树形DP)

    题目: A school bought the first computer some time ago(so this computer's id is 1). During the recent ...

  9. hdu 2196【树形dp】

    http://acm.hdu.edu.cn/showproblem.php?pid=2196 题意:找出树中每个节点到其它点的最远距离. 题解: 首先这是一棵树,对于节点v来说,它到达其它点的最远距离 ...

  10. HDU 2196 Compute --树形dp

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

随机推荐

  1. 链接Eclipse和SQL SEVER

    一.本文内容 讲诉使用JDBC建立Eclipse和Sql sever的桥梁的过程与其中可能遇见的问题. 二.详细内容 1.JDBC驱动的下载 建议一定要上与SQL SEVER相关的微软官网下载,以便找 ...

  2. What we learned in Seoul with AlphaGo

    What we learned in Seoul with AlphaGo March 16, 2016 Go isn’t just a game—it’s a living, breathing c ...

  3. [转载]深入理解ASP.NET MVC之ActionResult

    Action全局观 在上一篇最后,我们进行到了Action调用的“门口”: 1 if (!ActionInvoker.InvokeAction(ControllerContext, actionNam ...

  4. spoj 2148

    看似很水  却wa了好多遍   spoj上果然没有一下可以水过去的题....... #include<cstdio> #include<cstring> #include< ...

  5. 学习javascript总结下来的性能优化的小知识(一)

    http://www.cnblogs.com/ctriphire/p/4115525.html http://www.cnblogs.com/xjchenhao/archive/2012/10/22/ ...

  6. eclipse颜色配置

    Eclipse颜色主题插件:Eclipse Color Theme http://blog.sina.com.cn/s/blog_674212810101go8x.html 一个很赞的eclipse插 ...

  7. FZU-1921+线段树

    简单的线段树. 记录MinVal 和 相应的ID即可 /* 线段树 */ #include<stdio.h> #include<string.h> #include<st ...

  8. C++不同进制整数

    在C++的整数常量中,整数分为十进制整数.八进制整数和十六进制整数. 那给出一个整型常量怎样区分是何种进制呢?/给出一个整型常量,如100,默认是十进制数,如果在该数前加0,如0100,则此数表示为八 ...

  9. Qt: 内建对话框(各种对话框都有了,且用到了qobject_cast解析sender的技术)

    #include "BuiltinDialog.h" #include <QtGui/QTextEdit> #include <QtGui/QPushButton ...

  10. java:I/O 往原文件追加内容

    原来txt内容: "我要添加内容" import java.io.*; class Test { public static void main(String[] args) { ...