CF633F The Chocolate Spree
Description
Alice and Bob have a tree (undirected acyclic connected graph). There are \(a_{i}\) chocolates waiting to be picked up in the \(i-th\) vertex of the tree. First, they choose two different vertices as their starting positions (Alice chooses first) and take all the chocolates contained in them.
Then, they alternate their moves, selecting one vertex at a time and collecting all chocolates from this node. To make things more interesting, they decided that one can select a vertex only if he/she selected a vertex adjacent to that one at his/her previous turn and this vertex has not been already chosen by any of them during other move.
If at any moment one of them is not able to select the node that satisfy all the rules, he/she will skip his turns and let the other person pick chocolates as long as he/she can. This goes on until both of them cannot pick chocolates any further.
Due to their greed for chocolates, they want to collect as many chocolates as possible. However, as they are friends they only care about the total number of chocolates they obtain together. What is the maximum total number of chocolates they may pick?
Solution
其实这个题有比较套路的做法, 参考OO0OO0...或者tourist的提交
但是有一个童鞋提供了一个比较正常的做法godspeedkaka's blog.
Code
// my id of codeforces is XXXXXXXXX
#include <vector>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
const int N = 100005;
std:: vector<int> e[N];
int val[N];
long long Res[N], AChain[N], ACFNAAC[N], LCFNTL[N];
int GetAnswer(int u, int fa) {
Res[u] = AChain[u] = ACFNAAC[u] = LCFNTL[u] = val[u];
long long LongestChain = 0;
for (auto v : e[u]) {
if (v == fa) continue;
GetAnswer(v, u);
Res[u] = std:: max(Res[u], Res[v]);
Res[u] = std:: max(Res[u], AChain[u] + AChain[v]);
Res[u] = std:: max(Res[u], ACFNAAC[u] + LCFNTL[v]);
Res[u] = std:: max(Res[u], ACFNAAC[v] + LCFNTL[u]);
AChain[u] = std:: max(AChain[u], AChain[v]);
AChain[u] = std:: max(AChain[u], LCFNTL[u] + LCFNTL[v]);
ACFNAAC[u] = std:: max(ACFNAAC[u], val[u] + ACFNAAC[v]);
ACFNAAC[u] = std:: max(ACFNAAC[u], LCFNTL[u] + AChain[v]);
ACFNAAC[u] = std:: max(ACFNAAC[u], LCFNTL[v] + val[u] + LongestChain);
LongestChain = std:: max(LongestChain, AChain[v]);
LCFNTL[u] = std:: max(LCFNTL[u], LCFNTL[v] + val[u]);
}
}
int main () {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i += 1)
scanf("%d", &val[i]);
for (int i = 1; i < n; i += 1) {
int u, v;
scanf("%d%d", &u, &v);
e[u].push_back(v), e[v].push_back(u);
}
GetAnswer(1, 0);
printf("%I64d", Res[1]);
return 0;
}
CF633F The Chocolate Spree的更多相关文章
- cf633F. The Chocolate Spree(树形dp)
题意 题目链接 \(n\)个节点的树,点有点权,找出互不相交的两条链,使得权值和最大 Sol 这辈子也不会写树形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 ...
- codeforces 633F The Chocolate Spree (树形dp)
题目链接:http://codeforces.com/problemset/problem/633/F 题解:看起来很像是树形dp其实就是单纯的树上递归,就是挺难想到的. 显然要求最优解肯定是取最大的 ...
- Codeforces 633F - The Chocolate Spree(树形 dp)
Codeforces 题目传送门 & 洛谷题目传送门 看来我这个蒟蒻现在也只配刷刷 *2600 左右的题了/dk 这里提供一个奇奇怪怪的大常数做法. 首先还是考虑分析"两条不相交路径 ...
- Solution -「树上杂题?」专练
主要是记录思路,不要被刚开始错误方向带偏了 www 「CF1110F」Nearest Leaf 特殊性质:先序遍历即为 \(1 \to n\),可得出:叶子节点编号递增或可在不改变树形态的基础上调整为 ...
- Manthan, Codefest 16
暴力 A - Ebony and Ivory import java.util.*; import java.io.*; public class Main { public static void ...
- Big Chocolate
Big Chocolate 题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=19127 Big Chocolat ...
- Dividing a Chocolate(zoj 2705)
Dividing a Chocolate zoj 2705 递推,找规律的题目: 具体思路见:http://blog.csdn.net/u010770930/article/details/97693 ...
随机推荐
- POJ1743:Musical Theme——题解
http://poj.org/problem?id=1743 给一段数,求最大相似子串长度,如果没有输出0. 相似子串定义: 1.两个不重叠的子串,其中一个是另一个加/减一个数得来的. 2.长度> ...
- BZOJ4946 & 洛谷3826 & UOJ318:[NOI2017]蔬菜——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4946 https://www.luogu.org/problemnew/show/P3826 ht ...
- BZOJ2821:作诗——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=2821 问题描述 神犇SJY虐完HEOI之后给傻×LYD出了一题: SHY是T国的公主,平时的一大爱好 ...
- B树,B+树,B*树简介
B树(有些人也叫B-树) 是一种多路搜索树 : 1.定义任意非叶子结点最多只有M个儿子:且M>2: 2.根结点的儿子数为[2, M]: 3.除根结点以外的非叶子结点的儿子数为[M/2, M]: ...
- webpack优化总结
1. 分包 将不需要变动的第三方包分离出去, 主要方法有: (1). externals(2). DllPlugin(3). expose-loader(4). ProviderPlugin 2. 拆 ...
- exBSGS板子
/*bzoj2480*/ #include <map> #include <cmath> #include <cstdio> #include <cstrin ...
- node记录
集中管理 require('sequelize'); require('node-schedule')
- xshell如何让窗口并列排序
如图所示:
- mybatis的模糊查询写法
mybatis做like模糊查询 1. 参数中直接加入%% param.setUsername("%CD%"); param.setPassword("% ...
- bzoj 3289 Mato的文件管理 树状数组+莫队
Mato的文件管理 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 4325 Solved: 1757[Submit][Status][Discuss ...