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 ...
随机推荐
- ASP.NET MVC Spring.NET 整合
请注明转载地址:http://www.cnblogs.com/arhat 在整合这三个技术之前,首先得说明一下整合的步骤,俗话说汗要一口一口吃,事要一件一件做.同理这个三个技术也是.那么在整合之前,需 ...
- iOS多工程依赖
导入准备工作 1.建立一个Framework & Library->Cocoa Touch Static Library 取名A,并在A里新建一个类用来测试 2.建立一个demo工程B ...
- dos下的edit命令使用详解
dos下的edit命令使用详解 来源:网络 作者:未知 edit命令是一个简单的编辑软件,我们经常用它来编辑一些程序和批处理文件. 比如,我想在c盘根目录下编辑一个简单的批处理文件,要求无论当前盘和当 ...
- poj 3261 Milk Patterns(后缀数组)(k次的最长重复子串)
Milk Patterns Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7938 Accepted: 3598 Cas ...
- CSS3 animation的steps方式过渡
animation默认以ease方式过渡,它会在每个关键帧之间插入补间动画,所以动画效果 是连贯性的.除了ease,linear.cubic-bezier之类的过渡函数都会为其插入补间. 但有些效果不 ...
- 创建型-生成器模式(Builder)
1.意图: 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. 2.场景描述: 编辑软件的“另存为”功能便是生成器模式的一个体现.例如,Word的另存为功能,可以选择将文件存储 ...
- C++类中静态变量和静态方法使用介绍!
原链接:http://www.ecoviews.cn/net/archives/216.html 静态成员的提出是为了解决数据共享的问题.实现共享有许多方法,如:设置全局性的变量或对象是一种方法.但是 ...
- 关于HSL和HSV颜色空间的详细论述
目前在计算机视觉领域存在着较多类型的颜色空间(color space).HSL和HSV是两种最常见的圆柱坐标表示的颜色模型,它重新影射了RGB模型,从而能够视觉上比RGB模型更具有视觉直观性. HSV ...
- Sequence Assignments FRM-41830: List of Value contains no entries.
SYMPTOMS ============================================================ GL > Setup > Financials ...
- USACO4.12Beef McNuggets(背包+数论)
昨天晚上写的一题 结果USACO一直挂中 今天交了下 有一点点的数论知识 背包很好想 就是不好确定上界 官方题解: 这是一个背包问题.一般使用动态规划求解. 一种具体的实现是:用一个线性表储存所有的 ...