zoukankan      html  css  js  c++  java
  • bzoj1211: [HNOI2004]树的计数 prufer序列裸题

    一个有n个结点的树,设它的结点分别为v1, v2, …, vn,已知第i个结点vi的度数为di,问满足这样的条件的不同的树有多少棵。给定n,d1, d2, …, dn,编程需要输出满足d(vi)=di的树的个数。

    答案是(n-2)!/(a[1]-1)!/.../(a[n]-1)!,要特判一下不满足的情况和n==1的情况

    /**************************************************************
        Problem: 1211
        User: walfy
        Language: Java
        Result: Accepted
        Time:560 ms
        Memory:17892 kb
    ****************************************************************/
     
    import java.math.BigInteger;
    import java.util.*;
     
    public class Main {
     
        public static void main(String[] args) {
            Scanner cin = new Scanner(System.in);
            int n = cin.nextInt();
            int[] a = new int [200];
            boolean ok = true;
            int sum=0;
            for(int i=0;i<n;i++)
            {
                a[i]=cin.nextInt();
                if(a[i] == 0)ok=false;
                sum += a[i];
            }
            if(n==1)
            {
                if(a[0]==0)System.out.println(1);
                else System.out.println(0);
                return ;
            }
            if(ok == false||sum != 2*n-2)
            {
                System.out.println(0);
                return ;
            }
            BigInteger [] b = new BigInteger [200];
            b[0] = BigInteger.ONE;
            for(int i=1;i<200;i++)
            {
                b[i]=b[i-1].multiply(BigInteger.valueOf(i));
            }
            BigInteger ans = b[n-2];
            for(int i=0;i<n;i++)
            {
                ans=ans.divide(b[a[i]-1]);
            }
            System.out.println(ans);
        }
    }
    View Code
  • 相关阅读:
    uva 11078
    hdu1520(树状dp)
    从Markov Process到Markov Decision Process
    剑指Offer系列编程题详解全集
    L1正则和L2正则的区别详解
    协方差详解
    牛顿法和梯度下降法的比较
    C++ const各种用法总结
    Exploration and Exploitation
    RL Algorithm Components
  • 原文地址:https://www.cnblogs.com/acjiumeng/p/9068729.html
Copyright © 2011-2022 走看看