AmazonOA #266
Maximize Product of Sizes of Subtrees
Estimated HardJudge-ready
Topics (AI-labelled)KnapsackBits & Math
Given an undirected tree, delete at least one edge, and as many as you like, forming several connected components. Each component's size is its number of nodes; find the maximum product of all component sizes. There are no node weights, the answer is not taken modulo anything, and not cutting at all is not allowed. The original text of this page explicitly says one or more, and examples from another, conflicting problem page must not replace this page's rule.
Input
The first line contains n, followed by n−1 lines each containing u v; nodes are numbered 1..n and are guaranteed to form a tree. The original text had no numeric bounds; this site uses 2≤n≤2000, and the output is an exact integer that may exceed 64 bits.
Output
Output a single integer, the answer.
Example 1
Input:
5
1 2
2 3
3 4
4 5
Output:
6
Example 2
Input:
2
1 2
Output:
1
Example 3
Input:
6
1 2
1 3
1 4
1 5
1 6
Output:
5
Example notes
The answers are 6, 1 and 5. A star cannot be split arbitrarily into two 3-node components as in integer partitioning; a two-node tree must be cut, giving 1.
Editorial
Loading solution…