HDU 2196 树形DP Computer
题目链接: HDU 2196 Computer
分析: 先从任意一点开始, 求出它到其它点的最大距离, 然后以该点为中心更新它的邻点,
再用被更新的点去更新邻点......依此递推 !
代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <iomanip> using namespace std;
const int inf = 0x7FFFFFFF;
const int maxn = 11111; struct node{
int to, dix, sum;
node *next;
}tree[maxn<<1], *head[maxn]; int ptr, n;
bool vis[maxn];
int dp[maxn],h[maxn]; void Init(){
ptr=1;
memset(dp,0,sizeof(dp));
memset(vis,false,sizeof(vis));
memset(head,0,sizeof(head));
} void AddEdge(int x,int y,int s){
tree[ptr].dix=s;
tree[ptr].to=y;
tree[ptr].next=head[x];
head[x]=&tree[ptr++];
} void DFS(int cnt){
vis[cnt]=true;
node *p=head[cnt];
while(p!=NULL){
if(vis[p->to]) {
p=p->next; continue;
}
DFS(p->to);
dp[cnt]=max(dp[cnt],dp[p->to]+p->dix);
p->sum=dp[p->to]+p->dix;
p=p->next;
}
} void Tree_Dp(int father, int son){
if(vis[son]) return ;
vis[son]=true;
int Max=0;
node* p=head[father];
while(p!=NULL){
if(p->to!=son)
Max=max(Max,p->sum);
p=p->next;
}
p=head[son];
while(p!=NULL){
if(p->to==father){
p->sum=p->dix+Max; break;
}
p=p->next;
}
p=head[son];
while(p!=NULL){
dp[son]=max(dp[son],p->sum);
Tree_Dp(son,p->to);
p=p->next;
}
}
int main(){
while(~scanf("%d",&n)&&n){
Init();
for(int i=2;i<=n;++i){
int a,b; scanf("%d%d",&a,&b);
AddEdge(a,i,b);
AddEdge(i,a,b);
} DFS(1); ///得到1点到其它所有点的距离 memset(vis,false,sizeof(vis));
node* p=head[1];
while(p!=NULL){ ///从1的邻点开始更新
Tree_Dp(1,p->to);
p=p->next;
}
for(int i=1;i<=n;++i)
printf("%d\n",dp[i]);
}
return 0;
}
HDU 2196 树形DP Computer的更多相关文章
- HDU 2196树形DP(2个方向)
HDU 2196 [题目链接]HDU 2196 [题目类型]树形DP(2个方向) &题意: 题意是求树中每个点到所有叶子节点的距离的最大值是多少. &题解: 2次dfs,先把子树的最大 ...
- hdu 2196 树形dp
思路:先求以1为根时,每个节点到子节点的最大长度.然后再次从1进入进行更新. #include<iostream> #include<cstring> #include< ...
- hdu 4123 树形DP+RMQ
http://acm.hdu.edu.cn/showproblem.php? pid=4123 Problem Description Bob wants to hold a race to enco ...
- HDU 1520 树形dp裸题
1.HDU 1520 Anniversary party 2.总结:第一道树形dp,有点纠结 题意:公司聚会,员工与直接上司不能同时来,求最大权值和 #include<iostream> ...
- HDU 1561 树形DP入门
The more, The Better Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDU 1520 树形DP入门
HDU 1520 [题目链接]HDU 1520 [题目类型]树形DP &题意: 某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知 ...
- codevs 1380/HDU 1520 树形dp
1380 没有上司的舞会 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 回到问题 题目描述 Description Ural大学有N个职员 ...
- HDU 5834 [树形dp]
/* 题意:n个点组成的树,点和边都有权值,当第一次访问某个点的时候获得利益为点的权值 每次经过一条边,丢失利益为边的权值.问从第i个点出发,获得的利益最大是多少. 输入: 测试样例组数T n n个数 ...
- hdu 4267 树形DP
思路:先dfs一下,找出1,n间的路径长度和价值,回溯时将该路径长度和价值清零.那么对剩下的图就可以直接树形dp求解了. #include<iostream> #include<al ...
随机推荐
- Python 函数式编程学习
描述:通过将函数作为参数,使得功能类似的函数实现可以整合到同一个函数. Before def getAdd(lst): result = 0 for item in lst: result += it ...
- 《C和指针》章节后编程练习解答参考——第9章
9.1 #include <stdio.h> #include <ctype.h> #include <string.h> #define N 100 int ma ...
- hibernate的formula如何使用
之前用过hibernate的formula记得很好用,但是这次用到想不起来怎么用了,结果去网上查结果发现大多都是无用信息. 最终搞定了,还是在这里记录一下,省的忘记. 我用formula的目的在于字典 ...
- python的random函数
Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...
- csu 10月 月赛 A 题
Welcome to CSU OnlineJudge Problem A: Small change Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 15 ...
- 测来测去,感觉REQUESTS最实在
URLLIB,URLLIB2,PYCURL,HTTPIE,,,在测试PUT及认证时,还是REQUESTS胜出.. 测试过程及样例代码如下: import urllib import urllib2 i ...
- 有关TCP和UDP 粘包 消息保护边界
http://www.cnblogs.com/lancidie/archive/2013/10/28/3392428.html 在socket网络程序中,TCP和UDP分别是面向连接和非面向连接的.因 ...
- picturebox 图片自适应
picturebox控件共有两种载入图片方式,分别为: pictureBox1.BackgroundImage = Image,pictureBox1.load(url) 为使加载的图片自使用控件尺寸 ...
- 【HDOJ】1072 Nightmare
bfs,使用ttl进行剪枝. #include <iostream> #include <cstdio> #include <cstring> #include & ...
- POJ_2229_Sumsets_(动态规划)
描述 http://poj.org/problem?id=2229 将一个数n分解为2的幂之和共有几种分法? Sumsets Time Limit: 2000MS Memory Limit: 20 ...