#P11259. [GDKOI2023 普及组] 海星
[GDKOI2023 普及组] 海星
Problem Description
Xiao Ming wants to catch starfish on the seabed. The seabed is a tree-like structure, and the starfish are hidden inside it.
Given a tree with nodes, numbered , and the weight of node is denoted as .
A starfish is defined as a “flower subgraph” in the tree. Assume its center node is labeled , and its leaf (peripheral) nodes are labeled (each leaf node must be directly connected to the center node, and the flower subgraph contains at least one center node and one leaf node). The value of this starfish is defined as .
Xiao Ming wants to know the maximum total value of starfish he can catch. (He may catch multiple starfish at the same time, but the intersection of the node sets of any two starfish must be empty.)
Additional definitions:
Flower graph: A connected graph with diameter less than or equal to . The node with the largest degree is the center node, and all other nodes are leaf (peripheral) nodes (so every leaf node has degree ).
Example: The smallest flower graph is , which consists of only two nodes and the single edge connecting them.
Input Format
The first line contains a positive integer , representing the size of the tree.
The second line contains integers, representing .
Lines to each contain two integers , indicating that there is an edge between node and node .
Output Format
Output one line containing one integer, representing the maximum total value of starfish Xiao Ming can catch.
5
-2 -3 4 -5 1
1 2
1 3
2 4
2 5
10
见/example/starfish/下
见/example/starfish/下
Hint
Sample Explanation
One valid plan is that Xiao Ming catches two starfish. The first starfish has center node and leaf node , with value . The second starfish has center node and leaf node , with value . In this case, the maximum total value is .
Constraints
For of the testdata, the data is guaranteed to form a flower graph.
For of the testdata, the data is guaranteed to form a chain.
For of the testdata, it is guaranteed that the product of the weights of adjacent nodes is always negative.
For of the testdata, the data is guaranteed to form a binary tree rooted at .
For of the testdata, there are no special constraints.
For all testdata, , .
In the provided samples, data numbered and correspond to constraint group number , where .
Translated by ChatGPT 5