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. 如何提升集群资源利用率? 阿里容器调度系统Sigma 深入解析

    阿里妹导读:为了保证系统的在线交易服务顺利运转,最初几年,阿里都是在双11大促来临之前大量采购机器储备计算资源,导致了双11之后资源大量闲置点现象.是否能把计算任务与在线服务进行混合部署,在现有弹性资 ...

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

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

  3. HDOJ(HDU).2546 饭卡(DP 01背包)

    HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...

  4. Linux IO Scheduler

    一直都对linux的io调度算法不理解,这段时间一直都在看这方面的内容,下面是总结和整理的网络上面的内容.生产上如何建议自己压一下.以实际为准. 每个块设备或者块设备的分区,都对应有自身的请求队列(r ...

  5. HDU 3507 斜率优化dp

    Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)To ...

  6. HDU3068:最长回文(Manacher模板)

    最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  7. derby数据库windows自带的客户端

    本示例演示用windows自带的ij来操作derby数据库,包括建库,建表,插入数据,查询数据 首先要配置环境变量: 其次打开cmd输入如下图所示的命令: java代码如下: package com. ...

  8. Linux 守护进程创建原理及简易方法

    1:什么是Linux下的守护进程 Linux daemon是运行于后台常驻内存的一种特殊进程,周期性的执行或者等待trigger执行某个任务,与用户交互断开,独立于控制终端.一个守护进程的父进程是in ...

  9. ZooKeeper管理员指南(九)

    部署 这部分包含了部署ZooKeeper的信息和覆盖这些话题 系统要求 集群(多服务)安装 单服务和开发者安装 前两部分假定你对在例如数据中心的生产环境安装ZooKeeper有兴趣.最后一部分包含你在 ...

  10. mysql 压缩包免安装版 安转步骤

    一. 下载 MySQL 的官网下载地址:http://www.mysql.com/downloads/ 在这个下载界面会有几个版本的选择. 1. MySQL Community Server 社区版本 ...