ICPCCamp 2016 Day 8 - Makoto Soejima's Contest #4(Ropes-树的度数与方案数)

题意:已知一棵树,给定每个节点的度数(最多为3),求方案数。

ans=(n2)!i[(degree[i]1)!]

#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <functional>
#include <cstdlib>
#include <queue>
#include <stack>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p]) 
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (500000000000LL)
#define F (1000000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
typedef long long ll;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
    return x*f;
} 
ll n;
ll inv2=(F+1)/2;
int main() {
    cin>>n;
    ll ans=1;
    For(i,n-2) {
        ans*=i;
        ans%=F;
    }
    int t=0;
    For(i,n) {
        int a=read();t+=a;
        if (a==3) ans*=inv2;
        ans%=F;
    }
    if (t!=2*n-2) puts("0");
    else cout<<ans<<endl;


    return 0;
}

你可能感兴趣的:(ICPCCamp 2016 Day 8 - Makoto Soejima's Contest #4(Ropes-树的度数与方案数))