Codeforces 1172B (combinatorics)

Face questions

Gives a tree of n points, requires a circle painted on it, and the edge do not intersect, the arrangement drawing correspondence (i.e., different kinds of the same operator rotated), find the number of programs. FIG following four points tree \ (T: V = \ { 1,2,3,4 \}, E = \ {(1,2), (1,3), (2,4) \} \ ) solution:

Images from the original title cf

Pictures from codeforces Title Description

analysis

For subtree of x, we find sub-tree node x must be a continuous interval on the circle, otherwise the situation will appear the following figure

Provided deg [x] x represents the degree of

For non-root x:

There deg x [x] -1 son, the program has arranged son \ ((deg [x] -1)! \) species, and then inserted into the root with son son a position adjacent to an arbitrary total deg [x] empty, a total answer is \ ((deg [x] -1 )! \ times deg [x] = deg [x]! \)

For the root x:

x position itself can be optionally substituted on a circle, there are n have deg .x [x] son arranged scheme \ (n \ times deg [x ]! \)

Thus, the total number of programs \ (n \ times \ prod_ { i = 1} ^ n deg (i)! \)

Code

#include<iostream>
#include<cstdio>
#define maxn 200005
#define mod 998244353
using namespace std;
int n;
long long fact[maxn];
int deg[maxn];

int main(){
    int u,v;
    scanf("%d",&n);
    for(int i=1;i<n;i++){
        scanf("%d %d",&u,&v);
        deg[u]++;
        deg[v]++; 
    }
    fact[0]=1;
    for(int i=1;i<=n;i++){
        fact[i]=fact[i-1]*i%mod;
    }
    long long ans=1;
    for(int i=1;i<=n;i++){
        ans*=fact[deg[i]];
        ans%=mod;
    }
    ans*=n;
    ans%=mod;
    printf("%I64d\n",ans);
}

Guess you like

Origin www.cnblogs.com/birchtree/p/10990067.html