题目链接

hdu5834

题解

思路很粗犷,实现很难受

设\(f[i][0|1]\)表示向子树走回来或不回来的最大收益

设\(g[i][0|1]\)表示向父亲走走回来或不回来的最大收益

再设\(h[i]\)为\(f[i][0]\)的次优收益

对于\(f[i][1]\),贪心选择所有\(f[v][1] - 2 * w \ge 0\)的子树即可

对于\(f[i][0]\),贪心选择所有没有被选的子树的\(f[v][0] - w \le 0\)的最大值 或者 被选子树\(f[v][1] - 2 * w\)改成\(f[v][0] - w\)后多产生收益的最大值

同时维护次优\(h[v]\)

对于\(g[i][1]\),设父亲为\(v\),就等于\(f[v][1] + g[v][1]\)再减去\(i\)对\(f[v][1]\)所作出的贡献【因为往父亲走要忽视\(i\)这课子树】

对于\(g[i][0]\)也是类似的,但是由于忽视\(i\)这课子树后\(f[i][0]\)的决策可能发生改变,所以要在之前算好次优决策\(h[v]\)

这种树形dp简单题都做不出了

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long int
#define Redge(u) for (int k = head[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
using namespace std;
const int maxn = 100005,maxm = 100005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int head[maxn],ne = 2;
struct EDGE{int to,nxt,w;}ed[maxn << 1];
inline void build(int u,int v,int w){
ed[ne] = (EDGE){v,head[u],w}; head[u] = ne++;
ed[ne] = (EDGE){u,head[v],w}; head[v] = ne++;
}
int n,fa[maxn],d[maxn],w[maxn],f[maxn][2],g[maxn][2],h[maxn],way[maxn];
//cal son
void dfs1(int u){
f[u][0] = f[u][1] = w[u];
int mx = -INF,v,tmp,mx2 = -INF;
Redge(u) if ((to = ed[k].to) != fa[u]){
fa[to] = u; d[to] = ed[k].w; dfs1(to);
if (f[to][1] - 2 * d[to] >= 0){
f[u][1] += f[to][1] - 2 * d[to];
tmp = (f[to][0] - d[to]) - (f[to][1] - 2 * d[to]);
if (tmp > mx) mx2 = mx,mx = tmp,v = to;
else if (tmp > mx2) mx2 = tmp;
}
else if ((tmp = f[to][0] - d[to]) >= 0){
if (tmp > mx) mx2 = mx,mx = tmp,v = to;
else if (tmp > mx2) mx2 = tmp;
}
}
if (mx >= 0) f[u][0] = f[u][1] + mx,way[u] = v;
else f[u][0] = f[u][1],way[u] = 0;
if (mx2 >= 0) h[u] = f[u][1] + mx2;
else h[u] = f[u][1];
}
//cal father
void dfs2(int u){
int v = fa[u];
//back
if (f[u][1] - 2 * d[u] >= 0)
g[u][1] = max(0,f[v][1] + g[v][1] - (f[u][1] - 2 * d[u]) - 2 * d[u]);
else g[u][1] = max(0,f[v][1] + g[v][1] - 2 * d[u]);
//not back
if (f[u][1] - 2 * d[u] >= 0){
g[u][0] = max(0,f[v][1] + g[v][0] - (f[u][1] - 2 * d[u]) - d[u]);
if (way[v] == u)
g[u][0] = max(g[u][0],h[v] + g[v][1] - (f[u][1] - 2 * d[u]) - d[u]);
else g[u][0] = max(g[u][0],f[v][0] + g[v][1] - (f[u][1] - 2 * d[u]) - d[u]);
}
else{
g[u][0] = max(0,f[v][1] + g[v][0] - d[u]);
if (way[v] == u)
g[u][0] = max(g[u][0],h[v] + g[v][1] - d[u]);
else g[u][0] = max(g[u][0],f[v][0] + g[v][1] - d[u]);
}
Redge(u) if ((to = ed[k].to) != fa[u])
dfs2(to);
}
int main(){
int T = read();
REP(C,T){
n = read(); ne = 2;
REP(i,n) w[i] = read(),head[i] = 0;
int a,b,w;
for (int i = 1; i < n; i++){
a = read(); b = read(); w = read();
build(a,b,w);
}
dfs1(1);
dfs2(1);
printf("Case #%d:\n",C);
REP(i,n) printf("%d\n",max(f[i][1] + g[i][0],f[i][0] + g[i][1]));
}
return 0;
}

hdu5834 Magic boy Bi Luo with his excited tree 【树形dp】的更多相关文章

  1. 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: ...

  2. HDU5834 Magic boy Bi Luo with his excited tree (树形DP)

    题意:一棵树有点权和边权 从每个点出发 走过一条边要花费边权同时可以获得点权 边走几次就算几次花费 点权最多算一次 问每个点能获得的最大价值 题解:好吧 这才叫树形DP入门题 dp[i][0]表示从i ...

  3. 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 ...

  4. 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree

    // 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree // 题意:n个点的树,每个节点有权值为正,只能用一次,每条边有负权,可以 ...

  5. 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 ...

  6. HDU5834Magic boy Bi Luo with his excited tree 树形dp

    分析:典型的两遍dfs树形dp,先统计到子树的,再统计从祖先来的,dp[i][0]代表从从子树回来的最大值,dp[i][1]代表不回来,id[i]记录从i开始到哪不回来 吐槽:赛场上想到了状态,但是不 ...

  7. 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 ...

  8. 动态规划(树形DP):HDU 5834 Magic boy Bi Luo with his excited tree

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA8UAAAJbCAIAAABCS6G8AAAgAElEQVR4nOy9fXQcxZ0uXH/hc8i5N+

  9. 【树形动规】HDU 5834 Magic boy Bi Luo with his excited tree

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5834 题目大意: 一棵N个点的有根树,每个节点有价值ci,每条树边有费用di,节点的值只能取一次,边 ...

随机推荐

  1. python_42_文件补充

    m=['红烧肉\n','熘肝尖','西红柿炒鸡蛋','腊八粥','油焖大虾'] fname=input("请输入文件名:")#输入xxx f=open(fname,'w',enco ...

  2. curl_easy_setopt函数介绍

    本节主要介绍curl_easy_setopt中跟http相关的参数.注意本节的阐述都是以libcurl作为主体,其它为客体来阐述的. 1.     CURLOPT_URL 设置访问URL 2.     ...

  3. ES6 Reflect使用笔记

    Reflect Reflect 对象和Proxy对象一样, 为操作对象提供了新的API. 为什么使用 Reflect的方式来操作对象? 将 Object 对象上一些明显属于内部的方法放到 Reflec ...

  4. linux关于权限

    用户权限:drwxr-x---. 8 root root 4096 8月 6 23:18 mnt 第一个root:所有者 即root用户第二个root:所有者所在的组mnt:所有者创建的文件夹Rwx: ...

  5. 用jq给img添加error事件

    <img src="xxxx.jpg" alt="" /> <script> $(document).ready(function(){ ...

  6. OpenFaceswap 入门教程(3): 软件参数篇!

    OpenFaceswap 的使用可以说是非常简单,只要稍加点拨就可以学会,厉害一点的人根本不需要教程,直接自己点几下就知道了.看了前面安装篇和使用篇.我想大多数人应该会了. 当学会了使用之后,你可能对 ...

  7. BFS:Nightmare(可返回路径)

    解题心得: 1.point:关于可以返回路径的BFS的标记方法,并非是简单的0-1,而是可以用时间比较之后判断是否push. 2.queue创建的地点(初始化问题),在全局中创建queue在一次调用B ...

  8. BFS:CF356C-Compartments

    C. Compartments time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  9. Git-Git初始化

    创建版本库及第一次提交 通过如下操作来查看一下您的Git版本. $ git --version git version 1.7.4 在开始 Git 之旅之前,我们需要设置一下 Git 的配置变量,这是 ...

  10. 云中Active Directory是如何工作的?

    [TechTarget中国原创] 微软公司1999年在Windows Server 2000中引入Active Directory功能.后期的Windows Server版本中陆续进行改善提升,Win ...