HDU2196 - Computer(树形DP)
题目大意
给定一颗n个结点的树,编号为1~n,要求你求出每个结点能到达的最长路径
题解
用动态规划解决的~~~~把1 当成树根,这样就转换成有根树了。我们可以发现,对于每个结点的最长路,要么是从子树得到要么是从父亲得到我们用数组f,g,h分别记录从当前结点到子树能够获得的最大值和第二大值以及从当前结点到祖先能够获得的最大值。我们先进行一次dfs,把f和g值求出来,然后再进行一次dfs求出h的值来,对于h值,得分两种情况,对于当前结点s的h值,如果它的父亲得最长路径没有经过结点s,假设父亲结点为v,那么h的值为dis(s,v)+max(f[v],h[v]),否则的话h的值为dis(s,v)+max(g[v],h[v])
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
#define MAXN 10005
vector<int>ivec[MAXN],value[MAXN];
int f[MAXN],g[MAXN],h[MAXN];
int son[MAXN];
int dfs1(int root)
{
if(f[root]) return f[root];
if(ivec[root].empty()) return 0;
int len=ivec[root].size();
for(int i=0; i<len; i++)
if(dfs1(ivec[root][i])+value[root][i]>f[root])
{
f[root]=f[ivec[root][i]]+value[root][i];
son[root]=ivec[root][i];
}
for(int i=0; i<len; i++)
if(ivec[root][i]!=son[root]&&f[ivec[root][i]]+value[root][i]>g[root])
g[root]=f[ivec[root][i]]+value[root][i];
return f[root];
}
void dfs2(int root)
{
if(ivec[root].empty()) return;
int len=ivec[root].size();
for(int i=0; i<len; i++)
{
if(ivec[root][i]!=son[root])
h[ivec[root][i]]=value[root][i]+max(f[root],h[root]);
else
h[ivec[root][i]]=max(g[root],h[root])+value[root][i];
dfs2(ivec[root][i]);
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=1; i<=n; i++)
{
ivec[i].clear();
value[i].clear();
}
for(int i=2; i<=n; i++)
{
int a,b;
scanf("%d%d",&a,&b);
ivec[a].push_back(i);
value[a].push_back(b);
}
memset(f,0,sizeof(f));
memset(g,0,sizeof(g));
memset(h,0,sizeof(h));
f[1]=dfs1(1);
dfs2(1);
for(int i=1;i<=n;i++)
printf("%d\n",max(f[i],h[i]));
}
return 0;
}
HDU2196 - Computer(树形DP)的更多相关文章
- HDU 2196.Computer 树形dp 树的直径
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- computer(树形dp || 树的直径)
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 2196 Computer 树形DP 经典题
给出一棵树,边有权值,求出离每一个节点最远的点的距离 树形DP,经典题 本来这道题是无根树,可以随意选择root, 但是根据输入数据的方式,选择root=1明显可以方便很多. 我们先把边权转化为点权, ...
- HDU 2196 Computer 树形DP经典题
链接:http://acm.hdu.edu.cn/showproblem.php? pid=2196 题意:每一个电脑都用线连接到了还有一台电脑,连接用的线有一定的长度,最后把全部电脑连成了一棵树,问 ...
- 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)
A school bought the first computer some time ago(so this computer's id is 1). During the recent year ...
- hdu-2169 Computer(树形dp+树的直径)
题目链接: Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 【HDU 2196】 Computer (树形DP)
[HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...
- hdu 2196 Computer 树形dp模板题
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
随机推荐
- 常用面试sql语句
1.编写一条sql语句,要修改一个字段的俩个值,比如把字段sex中的男改为女,女改为男. update m set m=(case when m='男' then '女' else '男' end) ...
- 在图层上使用CATransform3D制做三维动画-b
在UIView上,我们可以使用CGAffineTransform来对视图进行:平移(translation),旋转(Rotation),缩 放(scale),倾斜(Invert)操作,但这些操作是没有 ...
- vs运行代码版本不一致删除缓存
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files
- zepto源码学习-05 ajax
学习zeptoajax之前需要先脑补下,强烈推荐此文http://www.cnblogs.com/heyuquan/archive/2013/05/13/js-jquery-ajax.html 还有A ...
- POJ 2492 A Bug's Life(并查集)
http://poj.org/problem?id=2492 题意 :就是给你n条虫子,m对关系,每一对关系的双方都是异性的,让你找出有没有是同性恋的. 思路 :这个题跟POJ1703其实差不多,也是 ...
- php or die用法
当前面的函数运行出错时,终止并输入提示.常见的用法如:mysql_connect('locahost','root','') or die('数据库连接失败');
- ASP.NET MVC 入门介绍 (上)
MVC模式 MVC模式是一种软件架构模式.它把软件系统分为三个部分:模型(Model),视图(View)和控制器(Controller).MVC模式最早由Trygve Reenskaug在1974年提 ...
- 9. MonoBehaviour.StartCoroutine 开始协同程序
function StartCoroutine (routine : IEnumerator) : Coroutine 描述:开始协同程序. 一个协同程序在执行过程中,可以在任意位置使用yield语句 ...
- java.utils.HashMap数据结构分析(转)
上图为Hashmap的数据结构图,具体实线是采用数组结合链表实现,链表是为了解决在hash过程中因hash值一样导致的碰撞问题. 所以在使用自定义对象做key的时候,一定要去实现hashcode方 ...
- linux编译安装git
我的博客:www.while0.com 用的centos6.4中自带的git,版本为1.7.1,配置好github的sshkey后,clone下来的项目无法提交,提示: fatal: Unable t ...