HDU 5834 Magic boy Bi Luo with his excited tree
树形dp。
先dfs一次处理子树上的最优解,记录一下回到这个点和不回到这个点的最优解。
然后从上到下可以推出所有答案。细节较多,很容易写错。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
#include<ctime>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0);
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
} const int maxn=;
vector<int>G[maxn],t[][maxn][];
int T,n,v[maxn],dp[maxn][],ans[maxn],sz;
struct Edge{int u,v,w;}e[*maxn];
bool flag[maxn]; void add(int a,int b,int c)
{
e[sz].u=a; e[sz].v=b; e[sz].w=c;
G[a].push_back(sz++);
} void dfs(int x,int f)
{
flag[x]=; dp[x][]=dp[x][]=v[x]; int i; if(f==) i=; else i=G[x].size()-;
while()
{
if(f==&&i==G[x].size()) break; if(f==&&i==-) break; int id=G[x][i],to=e[id].v;
if(flag[to])
{
t[f][x][].push_back(dp[x][]); t[f][x][].push_back(dp[x][]);
if(f==) i++; else i--; continue;
} dfs(to,f); int t1=dp[x][], t2=dp[x][]+dp[to][]-e[id].w, t3=dp[x][]+dp[to][]-*e[id].w;
dp[x][]=max(t1,max(t2,t3));
dp[x][]=dp[x][]+max(dp[to][]-*e[id].w,); t[f][x][].push_back(dp[x][]); t[f][x][].push_back(dp[x][]);
if(f==) i++; else i--;
} } void get(int x,int a,int b,int c)
{
flag[x]=; int t1=dp[x][];
int t2=dp[x][]+b-c;
int t3=a+dp[x][]-*c; ans[x]=max(t1,max(t2,t3)); int SZ=G[x].size(); for(int i=;i<SZ;i++)
{
int id=G[x][i],to=e[id].v;
if(flag[to]) continue;
int na=,nb=,dp0=,dp1=, x1=,x2=,y1=,y2=;
if(i->=) x1=t[][x][][i-];
if(SZ--(i+)>=) x2=t[][x][][SZ--(i+)];
if(i->=) y1=t[][x][][i-];
if(SZ--(i+)>=) y2=t[][x][][SZ--(i+)];
dp0=x1+x2; if(x1!=&&x2!=) dp0=dp0-v[x];
int k1,k2;
k1=x1+y2; if(x1!=&&y2!=) k1=k1-v[x];
k2=x2+y1; if(x2!=&&y1!=) k2=k2-v[x];
dp1=max(k1,k2);
dp0=max(dp0,v[x]); dp1=max(dp1,v[x]);
na=max(dp0,dp0-*c+a); nb=max(dp1,max(dp0+b-c,a+dp1-*c));
get(to,na,nb,e[id].w); } } int main()
{
scanf("%d",&T); int cas=;
while(T--)
{
scanf("%d",&n); sz=; memset(dp,,sizeof dp);
for(int i=;i<=n;i++)
{
G[i].clear();
t[][i][].clear(); t[][i][].clear();
t[][i][].clear(); t[][i][].clear();
} for(int i=;i<=n;i++) scanf("%d",&v[i]);
for(int i=;i<=n-;i++)
{
int u,v,w; scanf("%d%d%d",&u,&v,&w);
add(u,v,w); add(v,u,w);
} memset(flag,,sizeof flag); dfs(,);
memset(flag,,sizeof flag); dfs(,); memset(flag,,sizeof flag); get(,,,); printf("Case #%d:\n",cas++);
for(int i=;i<=n;i++) printf("%d\n",ans[i]);
}
return ;
}
HDU 5834 Magic boy Bi Luo with his excited tree的更多相关文章
- hdu 5834 Magic boy Bi Luo with his excited tree 树形dp+转移
Magic boy Bi Luo with his excited tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 13107 ...
- 【树形动规】HDU 5834 Magic boy Bi Luo with his excited tree
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5834 题目大意: 一棵N个点的有根树,每个节点有价值ci,每条树边有费用di,节点的值只能取一次,边 ...
- HDU 5834 Magic boy Bi Luo with his excited tree(树形dp)
http://acm.hdu.edu.cn/showproblem.php?pid=5834 题意: 一棵树上每个节点有一个价值$Vi$,每个节点只能获得一次,每走一次一条边要花费$Ci$,问从各个节 ...
- 动态规划(树形DP):HDU 5834 Magic boy Bi Luo with his excited tree
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA8UAAAJbCAIAAABCS6G8AAAgAElEQVR4nOy9fXQcxZ0uXH/hc8i5N+
- 2016中国大学生程序设计竞赛 - 网络选拔赛 C. Magic boy Bi Luo with his excited tree
Magic boy Bi Luo with his excited tree Problem Description Bi Luo is a magic boy, he also has a migi ...
- hdu-5834 Magic boy Bi Luo with his excited tree(树形dp)
题目链接: Magic boy Bi Luo with his excited tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: ...
- 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree
// 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree // 题意:n个点的树,每个节点有权值为正,只能用一次,每条边有负权,可以 ...
- HDU5834 Magic boy Bi Luo with his excited tree(树形DP)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5834 Description Bi Luo is a magic boy, he also ...
- hdu5834 Magic boy Bi Luo with his excited tree 【树形dp】
题目链接 hdu5834 题解 思路很粗犷,实现很难受 设\(f[i][0|1]\)表示向子树走回来或不回来的最大收益 设\(g[i][0|1]\)表示向父亲走走回来或不回来的最大收益 再设\(h[i ...
随机推荐
- 结构-行为-样式-Js函数节流
最近一个面试官问了我一个函数节流的问题,然后感觉自己工作中遇到过这个问题,但是不知道这种形式就是函数节流.下面我来说下这个Js的高级问题,思路:函数节流就是防止用户高频调用某个事件而做的Js层面的处理 ...
- Oracle-11g 中两库间物化视图的同步
html,body { font-size: 15px } body { font-family: Helvetica, "Hiragino Sans GB", "微软雅 ...
- suse linux11 包括所有的linux操作系统的 遗忘root密码解决方案
2017-1-13号,用户要割接ocs系统应用,因为不能直接给root密码,但是操作过程中出现密码修改出错,再次登录系统仍然有问题.去机房熬了将近6个小时,试过单用户模式(但需要密码),试过光盘救援模 ...
- IMG图片垂直居中的问题
之前老是碰到图片文字位置调整的问题,图片不按自己的要求变化,后来发现其实很简单. <P><img src="" style="vertical-alig ...
- Lua 中的string库(字符串函数库)总结
(字符串函数库)总结 投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014-11-20我要评论 这篇文章主要介绍了Lua中的string库(字符串函数库)总结,本文讲解了string库 ...
- yarn的调度器
三种调度器 1.FIFO Scheduler 把应用按提交的顺序排成一个队列,这是一个先进先出队列,在进行资源分配的时候,先给队列中最头上的应用进行分配资源,等最前面的应用需求满足后再给下一个分配,以 ...
- 【python标准库】内建函数
abs(x) 返回一个数的绝对值.参数可以是普通的整数,长整数或者浮点数.如果参数是个复数,返回它的模. all(iterable) 如果iterable的所有元素为真(或者iterable为空), ...
- hdu1030
#include<stdio.h>#include<math.h>void find(int n,int &l,int &r,int &level){ ...
- 转:iOS 屏幕适配,autoResizing autoLayout和sizeClass图文详解
1. autoResizing autoresizing是苹果早期的ui布局适配的解决办法,iOS6之前完全可以胜任了,因为苹果手机只有3.5寸的屏幕,在加上手机app很少支持横屏,所以iOS开发者基 ...
- 洛谷-求同构数的个数-NOIP2013提高组复赛
题目描述 Description 所谓同构数是指这样的数,即它出现在它的平方数的右端.例如,5的平方是25 (即5×5=25),5是25右端的数,那么5就是同构数.又如,25的平方是625(即25×2 ...