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. 51nod 1197 字符串的数量 V2(矩阵快速幂+数论?)

    接上一篇,那个递推式显然可以用矩阵快速幂优化...自己随便YY了下就出来了,学了一下怎么用LaTeX画公式,LaTeX真是个好东西!嘿嘿嘿 如上图.(刚画错了一发...已更新 然后就可以过V2了 or ...

  2. 为什么使用centos部署服务器

    这个是实验室同学面试的时候,面试官问的一个问题? 为什么选择centos系统,为什么centos系统用的比较多呢? 首先我们说下redhat红帽公司,它是全球最大的linux服务提供商,它的服务是最好 ...

  3. 使用kubeadm安装Kubernetes 1.12

    使用kubeadm安装Kubernetes 1.12 https://blog.frognew.com/2018/10/kubeadm-install-kubernetes-1.12.html 测试环 ...

  4. React Mixin

    为什么使用 Mixin ? React为了将同样的功能添加到多个组件当中,你需要将这些通用的功能包装成一个mixin,然后导入到你的模块中. 可以说,相比继承而已,React更喜欢这种组合的方式. 写 ...

  5. Nginx简介及使用Nginx实现负载均衡的原理【通俗易懂,言简意赅】【转】

    Nginx 这个轻量级.高性能的 web server 主要可以干两件事情: 直接作为http server(代替apache,对PHP需要FastCGI处理器支持): 另外一个功能就是作为反向代理服 ...

  6. redis linux下的环境搭建

    系统  CentOS7 Redis 官网下载   https://redis.io/download 1.下载解压 [root@TestServer-DFJR programs]# /usr/loca ...

  7. Enterprise Architect13 : 去掉UML图页面右侧那一道竖线

    我们在使用Enterprise Architect 中,画用例图,时序图时,页面右侧有一条竖线,见下图: 如果页面元素太多,会超出竖线的范围,显得很不协调. 如果像去掉竖线,只需选择主菜单的Layou ...

  8. 【poj2947】高斯消元求解同模方程组【没有AC,存代码】

    题意: p start enda1,a2......ap (1<=ai<=n)第一行表示从星期start 到星期end 一共生产了p 件装饰物(工作的天数为end-start+1+7*x, ...

  9. Hadoop大数据生态系统及常用组件(山东数漫江湖)

    经过多年信息化建设,我们已经进入一个神奇的“大数据”时代,无论是在通讯社交过程中使用的微信.QQ.电话.短信,还是吃喝玩乐时的用到的团购.电商.移动支付,都不断产生海量信息数据,数据和我们的工作生活密 ...

  10. HDU 1422 重温世界杯 (dp)

    题目链接 Problem Description 世界杯结束了,意大利人连本带利的收回了法国人6年前欠他们的债,捧起了大力神杯,成就了4星意大利. 世界杯虽然结束了,但是这界世界杯给我们还是留下许多值 ...