题目链接

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. 2018.6.22 Java试题测试结果

    如何从有数字规律的网址抓取网页并保存在当前目录?假设网址为 http://test/0.xml,其中这个数字可以递增到100. for((i=0;i<100;++i));do wget http ...

  2. python_17_数据运算

    #//取整除,返回商的整数部分 print(9//2) print(10/3.3) print(10//3.0) #<>与!=都为不等于 #and 与 例(a and b) #or 或 # ...

  3. react的redux中间件

    redux 的中间件(对dispatch功能的拦截和增强,一般不用自己写,有插件) dispath是一个方法,可以自行中间拦截更改: store.dispatch = function(action) ...

  4. L2TP用户添加和删除、搜索脚本

    #!/bin/bash #author Template . /etc/init.d/functions DATE_TIME=$(date +%F-%T) FILE_PATH='/etc/ppp/ch ...

  5. 使用eclipse导入web项目

    第一步 第二步 第三步 第四步 最后就多了一个web项目

  6. php同一个用户同时只能登陆一个, 后登陆者踢掉前登陆者(排他登陆)

    通常用户登陆,如果没有特别的限定, 同一个用户可以同时登陆, 今天搞了一个东西限定一个用户不能同时登陆到一个系统上, 后登陆者会把前面登陆的踢出来.(有点像QQ,同个帐号不能在多个地方同时在线, 后面 ...

  7. du 与df 统计系统磁盘不一致原因与解决方法

    事件起因: 同事发现云主机磁盘系统盘满了,准备清理系统盘,便利用du 命令统计了根目录下各文件夹的大小,发现统计的各文件夹的大小总和 加起来比 df 命令查看到的系统盘所使用空间 要小很多.这里记录下 ...

  8. Python 代码优化技巧(一)

    Table of Contents 1. 代码优化Part1 1.1. if 判断的短路特性 1.2. join 合并字符串 1.3. while 1 和 while True 1.4. cProfi ...

  9. Spring MVC 使用 HttpServletResponseWrapper 修改返回结果

    HttpServletResponseWrapper 是什么? ServletResponse 的包装类,相关设计模式 装饰者模式. 运行环境 jdk 1.7 spring boot 整合的web环境 ...

  10. Java web学习总结

    javaweb学习总结(十四)——JSP原理 孤傲苍狼 2014-07-24 09:38 阅读:46603 评论:37     JavaWeb学习总结(十三)——使用Session防止表单重复提交 孤 ...