题目链接

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. 在O(1)时间复杂度删除链表节点

    题目描述: 给定一个单链表中的一个等待被删除的节点(非表头或表尾).请在在O(1)时间复杂度删除该链表节点. 您在真实的面试中是否遇到过这个题? Yes 样例 给定 1->2->3-> ...

  2. python_35_进度条

    import sys for i in range(50): sys.stdout.write("+")#此命令不会像print语句执行一次,换行一次\ sys.stdout.fl ...

  3. SqlServer触发器的理解

    SqlServer触发器是与表事件相关的特殊存储过程,它的执行不是由程序调用,也不是手工启动,而是由事件来触发.比如当对一个表进行操作( insert,delete, update)时就会激活它执行. ...

  4. 前端小记6——项目中常用的ES6方法

    现在很多功能用es5的方法也能实现功能,但es6提供的方法显得更为高效.记录下目前常用的几个方法. 1.字符包含 通过str.includes('a')来判断, 若str中包含a则结果为true,否则 ...

  5. IOS后台执行

    大多数应用程序进入后台状态不久后转入暂停状态.在这种状态下,应用程序不执行任何代码,并有可能在任意时候从内存中删除.应用程序提供特定的服务,用户可以请求后台执行时间,以提供这些服务. 判断是否支持多线 ...

  6. 牛客小白月赛5 F 圆(circle) 【欧拉定理】

    题目连接: https://www.nowcoder.com/acm/contest/135/F 签到题来了,送你们一个Python秒的题. Apojacsleam来到了OI大陆,经过了连年征战,成为 ...

  7. SpringBoot之HelloWorld仔细分析

    程序中的pom.xml文件: 一.父级标签 <parent> <groupId>org.springframework.boot</groupId> <art ...

  8. 认识mysql(3)

    认识mysql第三篇,发出的内容适合初学者,如果能持续关注我的博客,可以全面的掌握mysql的常用知识,后续我也会陆续发出python相关的知识,关注我,和我一共进步吧! 1.SQL查询 1.执行顺序 ...

  9. 静态属性property的本质和应用

    一.本质 静态属性property本质就是实现了get,set,delete三种方法 class Foo: @property def AAA(self): print('get的时候运行我啊') @ ...

  10. Find a path HDU - 5492 (dp)

    Find a path Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...