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 ...
随机推荐
- 用php 进行对文件的操作 (上)
如何让自己磁盘中的文件夹和目录显示在网页上?那就来看一下,用php是怎么来操作他们的吧 php中文件,一般包含两块内容,文件和目录 先来一句一句的看代码,及他的作用 运行后看一下结果 file 指的是 ...
- Python第一天——入门Python(2)字符串的简单操作
数据的操作 字符串的一些常用操作: 1 1 #!/usr/bin/env python 2 # #coding=utf-8 3 # 4 # test='hello world' 5 # print(t ...
- Oracle怎么更改用户名和密码
通过修改ORACLE基表的方式来修改用户名直接修改底层表USER$更改用户名(该方法在Oracle9i,Oracle10g中测试通过)SQL> UPDATE USER$ SET NAME='TT ...
- mysql解压版的配置安装
先在CMD进入编辑筐,用管理员身份运行 切换到mysql的解压目录的bin目录下并输入mysqld -install 这个时候启动服务, 发现出错!!! 检查这两个文件 这里的路径一定要核对 再次启动 ...
- 长安大学ACM竞赛部
本博客为长安大学ACM竞赛部的公共博客,记录长大ACMer的成长点滴. 开此博客,诸君共勉.
- 挖坑:CF712E
#include<cstdio> #include<cstring> #include<algorithm> #define maxn 1000005 using ...
- 【IE6的疯狂之六】li在IE中底部3像素的BUG(增加浮动解决问题)
今天开发项目中碰到一个li在IE中的BUG,先来看设计原型(如图:) 两个红色中间是<li>1px的底边框: 我写的代码如下: ============================== ...
- Unity3D脚本使用:物体调用物体
如下图4种方式: 方式5 通过Tag定位物体 1.先对物体定义标签Tag,可选已有或自定义 2.通过Tag名称找到对象 注意:如果标签对应多个对象,需使用获取对象集合再进行处理
- CodeForces 707D Persistent Bookcase
$dfs$,优化. $return$操作说明该操作完成之后的状态和经过操作$k$之后的状态是一样的.因此我们可以建树,然后从根节点开始$dfs$一次(回溯的时候复原一下状态)就可以算出所有状态的答案. ...
- auDemo
Option Explicit Private Const MODULE_NAME = "auDemo.WSC" Dim oEccomOperation,dbDemo Set oE ...