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 ...
随机推荐
- hdu 5654 xiaoxin and his watermelon candy 树状数组维护区间唯一元组
题目链接 题意:序列长度为n(1<= n <= 200,000)的序列,有Q(<=200,000)次区间查询,问区间[l,r]中有多少个不同的连续递增的三元组. 思路:连续三元组-& ...
- informix 查看数据库空间名
查看bhrs库的空间名 onstat -d 导出一个表 的结构 dbschema -d bhrs -t infotrans > xxx.sql 微网点 报表已经上线 cbs.sql 提交,生产 ...
- T[]与List<T>的使用时机
所有的数组类型都隐式地从System.Array这个抽象类派生,而System.Array又派生自System.Object.也就是说数组是引用类型.通过如下方式创建数组: int[] arrInt ...
- windows azure tools for mac
http://azure.microsoft.com/en-us/documentation/articles/xplat-cli/?fb=zh-cn http://www.windowsazure. ...
- android开发之---文字居中---android中去掉标题栏
1. 让textView里面的内容水平居中 : android:gravity="center_horizontal" 2. 让textView控件在它的父布局里水平居中 ...
- 上传项目到Github
1.使用根工具(均是图形化的界面) TortoiseGit-1.8.12.0-32bit GitExtensions-2.48.05-SetupComplete 2.大致步骤 首先,你需要一个Gith ...
- 1012: [JSOI2008]最大数maxnumber
单点更新,区间求最大值的题: 可以使用树状数组和线段树: #include<cstdio> #include<cstring> #include<algorithm> ...
- Ubuntu14.04强化之conky——Harmattan主题
一共有17个主题,四种模式,两种天气模式,支持摄氏度和华氏度. This conky comes with 17 themes: Cards Elementary Elune Flatts Metro ...
- BIOS
转自BIOS BIOS(Basic Input/Output System的缩写.中文:基本输入输出系统),在IBM PC兼容机上,是一种业界标准的固件接口.BIOS这个字眼是在1975第一次由CP/ ...
- (重)POJ 3020Antenna Placement
http://poj.org/problem?id=3020 呃...这个题不是很会,所以找了大神的博客做了参考,说得很详细 http://blog.csdn.net/lyy289065406/art ...