Tree
Time limit2sMemory limit1024 MB
Given a tree with integers on its nodes, pick two non-empty connected subgraphs with no shared node and no connecting edge, maximizing the total of their node values.
- Level
Medium6 of 10
- Topics
- Tree, Dynamic programming
- Solved
- No attempts yet
Problem
A tree has nodes. Each node has one integer written on it, and the integers can be negative. Find two subgraphs and of that satisfy the following conditions.
- ,
- and are each connected graphs.
- No edge in connects a node in with a node in .
- The sum of the integers on the nodes in plus the sum of the integers on the nodes in is maximized.
The figure below shows an example. .

The number on each node is its node number, and the value inside the node is the integer written on it. Several choices of and satisfy the conditions. Taking gives the maximum total of . No other choice gives a value greater than .
Function to implement
You need to write the following function.
long long findSum( int N, int C[], int Node1[], int Node2[] );
This function is called exactly once. The input is passed as arguments, and the return value is the answer. is the number of nodes. The nodes are numbered from to . For , the node numbered has the value . For , the node numbered and the node numbered are joined by an edge.