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的更多相关文章

  1. cf633F. The Chocolate Spree(树形dp)

    题意 题目链接 \(n\)个节点的树,点有点权,找出互不相交的两条链,使得权值和最大 Sol 这辈子也不会写树形dp的 也就是有几种情况,可以讨论一下.. 下文的"最大值"指的是& ...

  2. Codeforces 633F The Chocolate Spree 树形dp

    The Chocolate Spree 对拍拍了半天才知道哪里写错了.. dp[ i ][ j ][ k ]表示在 i 这棵子树中有 j 条链, 是否有链延伸上来. #include<bits/ ...

  3. CF 633 F. The Chocolate Spree 树形dp

    题目链接 CF 633 F. The Chocolate Spree 题解 维护子数答案 子数直径 子数最远点 单子数最长直径 (最长的 最远点+一条链) 讨论转移 代码 #include<ve ...

  4. codeforces 633F The Chocolate Spree (树形dp)

    题目链接:http://codeforces.com/problemset/problem/633/F 题解:看起来很像是树形dp其实就是单纯的树上递归,就是挺难想到的. 显然要求最优解肯定是取最大的 ...

  5. Codeforces 633F - The Chocolate Spree(树形 dp)

    Codeforces 题目传送门 & 洛谷题目传送门 看来我这个蒟蒻现在也只配刷刷 *2600 左右的题了/dk 这里提供一个奇奇怪怪的大常数做法. 首先还是考虑分析"两条不相交路径 ...

  6. Solution -「树上杂题?」专练

    主要是记录思路,不要被刚开始错误方向带偏了 www 「CF1110F」Nearest Leaf 特殊性质:先序遍历即为 \(1 \to n\),可得出:叶子节点编号递增或可在不改变树形态的基础上调整为 ...

  7. Manthan, Codefest 16

    暴力 A - Ebony and Ivory import java.util.*; import java.io.*; public class Main { public static void ...

  8. Big Chocolate

    Big Chocolate 题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=19127 Big Chocolat ...

  9. Dividing a Chocolate(zoj 2705)

    Dividing a Chocolate zoj 2705 递推,找规律的题目: 具体思路见:http://blog.csdn.net/u010770930/article/details/97693 ...

随机推荐

  1. Android 内核--Binder架构分析

    一.Binder架构 在Android中,Binder用于完成进程间通信(IPC),即把多个进程关联在一起.比如,普通应用程序可以调用音乐播放服务提供的播放.暂停.停止等功能.Binder工作在Lin ...

  2. POJ1816:Wild Words——题解

    http://poj.org/problem?id=1816 比较麻烦的trie. 首先你需要选择针对n还是m建立trie,这里我选择了针对n. 那么就需要面临卡空间的问题. 这里提供了一种链式前向星 ...

  3. BZOJ1861:[ZJOI2006]书架——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1861 (题面复制于洛谷) 题目描述 小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上 ...

  4. NOIP2017 列队——平衡树

    平衡树蒟蒻,敲了半天. 其实思路很简单,就是把许多个人合并成一个区间.必要的时候再拆开.(是不是和这个题的动态开点线段树有异曲同工之妙?) 每次操作最多多出来6个点. 理论上时间复杂度是nlogn,空 ...

  5. YBT 5.3 数位动态规划

    记忆化搜索的专题 题解在代码中 Amount of Degrees[loj 10163] /* 此题可以转换成将10进制转成b进制后有k个1其他都为0的个数 所以用记忆化dfs dp[pos][sum ...

  6. 工作中常用的Linux命令(不断更新中)

    最近工作中用到linux命令,简单总结如下: 1. pwd 查看当前所在的文件路径 2. cd 切换目录 cd .. 切换到上一级目录 3. ls 列出当前文件路径下的所有文件和文件夹 4. ll 是 ...

  7. C#或ASP.NET绘图初探

    C#或ASP.NET的简单绘图 public void ProcessRequest (HttpContext context) { context.Response.ContentType = &q ...

  8. git branch 重命名

    有时候你会有重命名一个git branch的冲动,不要怀疑,这是真的.command bellow will give u a big help,no thanks~ git branch - m o ...

  9. 【BZOJ】1574: [Usaco2009 Jan]地震损坏Damage

    [算法]搜索 [题意]给定无向图,现在可能有一些点已经被删除,只给出信息是c个点不能到达结点1,求最少的不能到达结点1的个数(含已删除点). [题解] 真是一道奥妙重重的题目. 每个点不能到达结点1, ...

  10. 简易微信小程序签到功能

    一.效果图 点击签到后 二.数据库 用一张数据表存用户签到的信息,每次用户签到都会往表中添加一条记录了用户id和签到日期的数据,如下图 三.后端 后端写两个接口,一个用于查询用户今日是否签到和签到记录 ...