bzoj 1211: [HNOI2004]树的计数

prufer的应用。。

详细见这篇博客:https://www.cnblogs.com/dirge/p/5503289.html

 1 import java.math.BigInteger;
 2 import java.util.*;
 3 public class Main {
 4     static long n, h;
 5     public static void main(String[] args) {
 6         Scanner in = new Scanner(System.in);
 7         BigInteger f[] = new BigInteger[151];
 8         f[0] = BigInteger.ONE;
 9         
10         int sum  = 0;
11         for(int i = 1; i <= 150; i++) {
12             f[i] = f[i - 1].multiply(BigInteger.valueOf(i));
13         }
14         
15         int n = in.nextInt();
16         
17         if(n == 1) {
18             int x = in.nextInt();
19             if(x == 0) System.out.println("1");
20             else System.out.println("0");
21         } else {
22             BigInteger ans = f[n - 2];
23             boolean flag = true;
24             for(int i = 1; i <= n; i++) {
25                 int x = in.nextInt();
26                 sum += x;
27                 if(x == 0 || x > 150) {
28                     flag = false;
29                 }
30                 else {
31                     ans = ans.divide(f[x - 1]);
32                 }
33             }
34             if(flag && sum == (n - 1) * 2) System.out.println(ans);
35             else System.out.println("0");
36         }
37         in.close();
38     }
39 }

 

posted @ 2018-05-24 16:57  NotNight  阅读(93)  评论(0编辑  收藏  举报