【BZOJ 1211】[HNOI2004] Counting of trees

【Link】 I am the link, click on me :)【Title】


enter question here

【answer】


The application of prufer sequence
http://www.cnblogs.com/AWCXV/p/7626625.html
This question has no degree of node uncertainty.
therefore.
The sum of the degree-1 of all nodes must be n-2.
Otherwise, there is no solution.

Then replace tot with n-2.
The approach is the same.
(The general idea is to select d[i] spaces from n-2 spaces to place node i, and select d[i+1] spaces from n-2-d[i] spaces to place node i+ 1
(Simplify it and it becomes the above formula.
(When d[i]==0, there is a solution when n==1
(you can solve this problem without high precision
(conclusion, n! Prime factor) After decomposition, the exponent of each prime factor p is ∑n/i where i is i,i^2,i^3...i^x where i^x<=n

【Code】

#include <bits/stdc++.h>
using namespace std;

const int N = 150;

int n,d[N+10],cnt[N+10];
bool is[N+10];

bool ok(int n){
    int len = sqrt(n);
    for (int i = 2;i <= len;i++)
        if (n%i==0)
            return false;
    return true;
}

void go(int n,int delta){
    for (int i = 1;i <= n;i++){
        if (is[i]){
            int sum = 0;
            for (int j = i;j <= n;j*=i) sum+=n/j;
            cnt[i]+=sum*delta;
        }
    }
}

int main(){
    scanf("%d",&n);
    int tot = 0;
    for (int i = 1;i <= n;i++){
        scanf("%d",&d[i]);
        if (d[i]==0 && n!=1) return puts("0"),0;
        d[i]--;
        tot+=d[i];
    }

    if (tot!=n-2) return puts("0"),0;

    for (int i = 2;i <= n;i++)
        if (ok(i)) is[i] = true;

    go(n-2,1);
    for (int i = 1;i <= n;i++) go(d[i],-1);
    long long temp = 1;
    for (int i = 1;i <= n;i++)
        for (int j = 1;j <= cnt[i];j++)
            temp = temp*i;
    printf("%lld\n",temp);
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325166374&siteId=291194637
Recommended