这个题的状态还是比较好设的。。
设d[i][j]为以i为根,有j个路径且还有根到叶子的半链的最大值
c[i][j]为以i为根,有j个路径的最大值
然后感觉是可以转移了,不过写的时候转移发现十分麻烦。。需要正确的姿势
将子树逐步合并,然后维护d和c,转移的时候需要另开一个g(为以i为根,有j个不经过i的路径的最大值)来辅助转移。。
然后都是细节问题了。。怎么转移见代码。。
链接:https://www.nowcoder.com/acm/contest/140/H
travel
时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
White Cloud has a tree with n nodes.The root is a node with number 1. Each node has a value.
White Rabbit wants to travel in the tree 3 times. In Each travel it will go through a path in the tree.
White Rabbit can't pass a node more than one time during the 3 travels. It wants to know the maximum sum value of all nodes it passes through.
输入描述:
The first line of input contains an integer n(3 <= n <= 400001) In the next line there are n integers in range [0,1000000] denoting the value of each node. For the next n-1 lines, each line contains two integers denoting the edge of this tree.
输出描述:
Print one integer denoting the answer.
示例1
输入
复制
13 10 10 10 10 10 1 10 10 10 1 10 10 10 1 2 2 3 3 4 4 5 2 6 6 7 7 8 7 9 6 10 10 11 11 12 11 13
输出
复制
110