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;
}

最新文章

  1. Linux下oracle环境变量无效问题
  2. Java语言程序设计(基础篇) 第五章 循环
  3. CentOs中yum安装LAMP+PHPMYADMIN
  4. codeforces 493A. Vasya and Football 解题报告
  5. Java堆和栈详解
  6. JAVA基础拾遗-论线程池的线程粒度划分与深浅放置
  7. 九、Foundation框架中的NSString常用方法
  8. imread() not working in OpenCV 2.4.11 Debug mode
  9. MySQL管理之道:性能调优、高可用与监控内置脚本
  10. 在Java中如何用String类中的indexof方法得到一个词的出现频率
  11. InetAddress Example program in Java
  12. [Winfrom] 捕获窗体最大化、最小化和关闭按钮的事件
  13. JS树型菜单
  14. PAT (Advanced Level) 1098. Insertion or Heap Sort (25)
  15. UE4利用Save Game创建全局变量
  16. 使用ML.NET实现猜动画片台词
  17. MySql cmd下的学习笔记 —— 有关多表查询的操作(多表查询练习题及union操作)
  18. Visual Studio(C#)快捷键与Eclipse(JAVA)快捷键对比
  19. Tomcat笔记:Tomcat的执行流程解析
  20. 第12月第1天 MASConstraintMaker crash

热门文章

  1. POJ1523:SPF——题解
  2. ZOJ3229:Shoot the Bullet——题解
  3. run (牛客多校第二场)计数DP
  4. 题解【bzoj2427 [HAOI2010]软件安装】
  5. HDU3579 线性同余方程(模板 余数不一定互质)
  6. Java设计模式の工厂模式
  7. rem自适应js代码
  8. python模拟android屏幕高频点击工具
  9. iOS排序
  10. Sass 条件-循环语句