A Tree Problem

View as PDF

Submit solution

Points: 25 (partial)
Time limit: 0.6s
Memory limit: 162M

Problem types
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

Count the number of distinct spanning trees that exist on N labeled vertices, given that some of these vertices are restricted in their degree.


1 \le N \le 1000

In test data worth 30% of marks, you may assume N \le 300.

Input Specification

The first line contains a single positive integer, N.

Each of the next N lines contains a single integer, d_i. If d_i is -1, then there is no degree restriction on vertex i. Otherwise, vertex i must have degree d_i.

Output Specification

Output the number of distinct spanning trees.

Sample Input


Sample Output



  • 10
    bqi343  commented on May 19, 2019, 9:52 a.m.

    Just read the formula at the end of this link.