#P2290. Counting Trees with a Given Degree Sequence
Counting Trees with a Given Degree Sequence
Counting Trees with a Given Degree Sequence
You are given a tree with n nodes labeled v1, v2, ..., vn. The degree of the i-th node is given as di. Your task is to determine the number of different trees that can have this degree sequence.
Recall that a sequence of degrees d1, d2, ..., dn can form a tree if and only if:
When the sequence is valid, the number of labeled trees with the given degree sequence is given by the formula:
For the special case when n = 1, the tree is valid only if d1 = 0, and the answer is 1.
inputFormat
The first line contains an integer n representing the number of nodes.
The second line contains n space-separated integers d1, d2, ..., dn, where di is the degree of node vi.
outputFormat
Output a single integer, the number of different trees that have the given degree sequence. If the sequence is invalid, output 0.
sample
3
1 1 2
1