zoukankan      html  css  js  c++  java
  • BZOJ1211: [HNOI2004]树的计数

    1211: [HNOI2004]树的计数

    Time Limit: 10 Sec  Memory Limit: 162 MB
    Submit: 1245  Solved: 383
    [Submit][Status]

    Description

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

    Input

    第一行是一个正整数n,表示树有n个结点。第二行有n个数,第i个数表示di,即树的第i个结点的度数。其中1<=n<=150,输入数据保证满足条件的树不超过10^17个。

    Output

    输出满足条件的树有多少棵。

    Sample Input

    4
    2 1 2 1

    Sample Output

    2

    HINT

    Source

    组合数学

    题解:

    prufer序列裸题吧。。。

    一直交上去0ms WA,实在受不鸟了交了lyd的代码。。。

    代码:

    mine

     1 #include<cstdio>
     2 
     3 #include<cstdlib>
     4 
     5 #include<cmath>
     6 
     7 #include<cstring>
     8 
     9 #include<algorithm>
    10 
    11 #include<iostream>
    12 
    13 #include<vector>
    14 
    15 #include<map>
    16 
    17 #include<set>
    18 
    19 #include<queue>
    20 
    21 #include<string>
    22 
    23 #define inf 1000000000
    24 
    25 #define maxn 50000
    26 
    27 #define maxm 500+100
    28 
    29 #define eps 1e-10
    30 
    31 #define ll long long
    32 
    33 #define pa pair<int,int>
    34 
    35 #define for0(i,n) for(int i=0;i<=(n);i++)
    36 
    37 #define for1(i,n) for(int i=1;i<=(n);i++)
    38 
    39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
    40 
    41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
    42 
    43 #define mod 1000000007
    44 
    45 using namespace std;
    46 
    47 inline int read()
    48 
    49 {
    50 
    51     int x=0,f=1;char ch=getchar();
    52 
    53     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    54 
    55     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
    56 
    57     return x*f;
    58 
    59 }
    60 int a[maxn],n,tot;
    61 bool v[maxn];
    62 
    63 int main()
    64 
    65 {
    66 
    67     freopen("input.txt","r",stdin);
    68 
    69     freopen("output.txt","w",stdout);
    70 
    71     n=read();
    72     int sum=0;
    73     for1(i,n)
    74      {
    75          int x=read()-1;sum+=x;
    76          if(x<0||x>=n){printf("0");return 0;};
    77          for1(j,x)a[++tot]=j;
    78      }
    79      if(n==1&&sum==-1){printf("1");return 0;}
    80      if(n==1&&sum==0){printf("0");return 0;}
    81     if(sum!=n-2){printf("0");return 0;}
    82     sort(a+1,a+tot+1);
    83     ll ans=1;
    84     for1(i,n-2)
    85     {
    86       ans*=i;
    87       for1(j,tot)
    88         if(!v[j]&&ans%a[j]==0)ans/=a[j],v[j]=1;
    89     } 
    90     printf("%lld
    ",ans);
    91 
    92     return 0;
    93 
    94 }
    View Code

    lyd

     1 #include<iostream>
     2 #include<cstdio>
     3 using namespace std;
     4 int n,m,c[200],i,j,sum;
     5 long long ans;
     6  
     7 int main()
     8 {
     9     freopen("input.txt","r",stdin);
    10 
    11     freopen("output2.txt","w",stdout);
    12     cin>>n;
    13     if(n==1) {cin>>m; if(m) cout<<0<<endl; else cout<<1<<endl; return 0;}
    14     for(i=1;i<=n;i++)
    15     {
    16         scanf("%d",&m);
    17         if(m<1||m>n) {cout<<0<<endl; return 0;}
    18         sum+=m-1;
    19         for(j=2;j<m;j++) c[j]++;
    20     }
    21     m=n-2;
    22     if(sum!=m) {cout<<0<<endl; return 0;}
    23     ans=1;
    24     for(i=2;i<=m;i++)
    25     {
    26         ans*=i;
    27         for(j=2;j<n;j++)
    28             while(c[j]&&ans%j==0) c[j]--,ans/=j;
    29     }
    30     cout<<ans<<endl;
    31     return 0;
    32 }
    View Code
  • 相关阅读:
    三种适配器模式 总结和使用场景
    (面试)Statement和PrepareStatement有什么区别
    知识点:Oracle+表连接方式(内连接-外连接-自连接)+详解 来自百度文库
    (面试题)有关Integer
    sessionId与cookie 的关系(百度文库)
    (面试)将1到100的随机数插入到长度为100的数组中,保证不会有重复元素
    如何通过sql的insert语句插入大量字符串到oracle的clob字段?
    (面试题)synchronized 和 java.util.concurrent.locks.Lock 的异同
    【转】java io 流 设计模式
    (面试题)两个对象值相同 (x.equals(y) == true) ,但却可有不同的 hash code ,这 句话对不对
  • 原文地址:https://www.cnblogs.com/zyfzyf/p/3979051.html
Copyright © 2011-2022 走看看