cf633F. The Chocolate Spree(树形dp)
题意
\(n\)个节点的树,点有点权,找出互不相交的两条链,使得权值和最大
Sol
这辈子也不会写树形dp的
也就是有几种情况,可以讨论一下。。
下文的“最大值”指的是“路径上权值和的最大值”
设\(f[i][0]\)表示以\(i\)为根的子树中选出两条不相交的链的最大值
\(f[i][1]\)表示以\(i\)为根的子树中选出一条链的最大值
\(g[i]\)表示以\(i\)为根的子树中从\(i\)到叶子节点 加上一条与之不相交的链的最大值
\(h[i]\)表示\(max{f[son][1]}\)
\(down[i]\)表示从\(u\)到叶子节点的最大值
现在最关键的是推出\(f[i][0]\)
转移的时候有四种情况
设当前儿子为\(v\)
在\(v\)中选两条不相交的链
在不含\(v\)的节点和以\(v\)为根的子树中各选一条链
down[i] + g[v] 也就是从该点和子树中分别选出半条链,再从子树内选出一条完整的链
g[i] + down[v] 与上面相反。
同时\(g, down\)也是可以推出来的。。
做完了。。慢慢调吧
#include<bits/stdc++.h>
#define chmax(a, b) (a = (a > b ? a : b))
#define chmin(a, b) (a = (a < b ? a : b))
#define LL long long
using namespace std;
const int MAXN = 100010;
inline int read() {
int x = 0, f = 1; char c = getchar();
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N;
LL a[MAXN], f[MAXN][2], g[MAXN], h[MAXN], down[MAXN];
vector<int> v[MAXN];
void dfs(int x, int fa) {
f[x][0] = f[x][1] = g[x] = down[x] = a[x];
for(int i = 0, to; i < v[x].size(); i++) {
if((to = v[x][i]) == fa) continue;
dfs(to, x);
chmax(f[x][0], f[to][0]);
chmax(f[x][0], f[x][1] + f[to][1]);
chmax(f[x][0], down[x] + g[to]);
chmax(f[x][0], down[to] + g[x]);
chmax(f[x][1], f[to][1]);
chmax(f[x][1], down[x] + down[to]);
chmax(g[x], g[to] + a[x]);
//chmax(g[x], down[to] + f[x][1]);
chmax(g[x], down[x] + f[to][1]);
chmax(g[x], down[to] + a[x] + h[x]);
chmax(h[x], f[to][1]);
chmax(down[x], a[x] + down[to]);
}
}
main() {
N = read();
for(int i = 1; i <= N; i++) a[i] = read();
for(int i = 1; i <= N - 1; i++) {
int x = read(), y = read();
v[x].push_back(y); v[y].push_back(x);
}
dfs(1, 0);
cout << f[1][0];
}
/*
*/
cf633F. The Chocolate Spree(树形dp)的更多相关文章
- Codeforces 633F The Chocolate Spree 树形dp
The Chocolate Spree 对拍拍了半天才知道哪里写错了.. dp[ i ][ j ][ k ]表示在 i 这棵子树中有 j 条链, 是否有链延伸上来. #include<bits/ ...
- CF 633 F. The Chocolate Spree 树形dp
题目链接 CF 633 F. The Chocolate Spree 题解 维护子数答案 子数直径 子数最远点 单子数最长直径 (最长的 最远点+一条链) 讨论转移 代码 #include<ve ...
- CF633F The Chocolate Spree
Description Alice and Bob have a tree (undirected acyclic connected graph). There are \(a_{i}\) choc ...
- codeforces 633F The Chocolate Spree (树形dp)
题目链接:http://codeforces.com/problemset/problem/633/F 题解:看起来很像是树形dp其实就是单纯的树上递归,就是挺难想到的. 显然要求最优解肯定是取最大的 ...
- poj3417 LCA + 树形dp
Network Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4478 Accepted: 1292 Descripti ...
- COGS 2532. [HZOI 2016]树之美 树形dp
可以发现这道题的数据范围有些奇怪,为毛n辣么大,而k只有10 我们从树形dp的角度来考虑这个问题. 如果我们设f[x][k]表示与x距离为k的点的数量,那么我们可以O(1)回答一个询问 可是这样的话d ...
- 【BZOJ-4726】Sabota? 树形DP
4726: [POI2017]Sabota? Time Limit: 20 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 128 Solved ...
- 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)
题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...
- 树形DP
切题ing!!!!! HDU 2196 Anniversary party 经典树形DP,以前写的太搓了,终于学会简单写法了.... #include <iostream> #inclu ...
随机推荐
- Mole and Abandoned Mine
Mole and Abandoned Mine n点m条边的无向图,删除第i条边花费c[i],问1到n只有一条路径时所需要的最小花费? \(2\le n\le 15\) . 我又A掉了一道zzs的题啦 ...
- [A/C 2007] 数据备份(网络流,堆)
[A/C 2007] 数据备份(网络流,堆) 给你N各点的位置和K条链,需要用这些链把2K个点连起来,使得链的总长最短.可以随意选择要链的点.n=100000. 这道题居然可以用堆-- 首先,不能把区 ...
- phaser小游戏框架学习(二)
今天继续学习phaser.js.上周写的学习教程主要内容是创建游戏场景,游戏中的显示对象,按钮对象的使用以及如何在不同屏幕大小中完美适配.这篇博客以介绍游戏榜单的渲染更新为主. 代码地址:https: ...
- Vue.js 60分钟快速入门
原文链接:http://www.cnblogs.com/keepfool/p/5619070.html Vue.js介绍 Vue.js是当下很火的一个JavaScript MVVM库,它是以数据驱动和 ...
- 2019.2.25考试T1, 矩阵快速幂加速递推+单位根反演(容斥)
\(\color{#0066ff}{题解}\) 然后a,b,c通过矩阵加速即可 为什么1出现偶数次3没出现的贡献是上面画绿线的部分呢? 考虑暴力统计这部分贡献,答案为\(\begin{aligned} ...
- 洛谷4316 绿豆蛙的归宿(DAG递推/概率dp)
题目大意: 给定一个DAG,求起点到终点的路径长度期望 根据题意可以知道每一条边都有一定概率被走到 那么\(\displaystyle\begin{aligned} Ans = \sum_{e \in ...
- main.obj:-1: error: LNK2001: 无法解析的外部符号 "public: virtual struct QMetaObject const * __thiscall CustomButton::metaObject(void)const " (?metaObject@CustomButton@@UBEPBUQMetaObject@@XZ)
QTCreator 运行时报错 main.obj:-1: error: LNK2001: 无法解析的外部符号 "public: virtual struct QMetaObject cons ...
- Educational Codeforces Round 7 B
Description You are given the current time in 24-hour format hh:mm. Find and print the time after a ...
- ZPL通用打印类
using System;using System.Collections.Generic;using System.IO;using System.Runtime.InteropServices;u ...
- Log4web独立config配置
第一步:config配置,独立文件的 <?xml version="1.0" encoding="utf-8"?> <configuratio ...