【题解】Luogu CF1172B Nauuo and Circle

Original title Portal

Meaning of the questions: There are n nodes on the circle (Ke to form a convex polygon), allowing you to node number, so that the title to your side (of a tree) did not cross

We handpicked 1 as the root of this tree. Any node \ (X \) point of a sub-tree should be continuous circular arc section(I do not know how I did it from HNOI2019d1t3 yy This question is to the polygon), It is, to some sequence into \ (siz [x] +1 \ ) segment, the contribution of the node is \ (A_ {SIZ [X]} ^ {+ 1'd SIZ [X] + 1'd} \) . Because the number of sorting between father and son program without disturbing each other, so the final answer is the product of all the nodes contributions (root node to special sentence, because in a ring, not directly number of permutations)

Written on the test code is relatively ugly

#include <bits/stdc++.h>
#define N 200005
#define mod 998244353
#define getchar nc
using namespace std;
inline char nc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int read()
{
    register int x=0,f=1;register char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
    return x*f;
}
inline void write(register int x)
{
    if(!x)putchar('0');if(x<0)x=-x,putchar('-');
    static int sta[20];register int tot=0;
    while(x)sta[tot++]=x%10,x/=10;
    while(tot)putchar(sta[--tot]+48);
}
struct edge{
    int to,next;
}e[N<<1];
int head[N],cnt;
inline void add(register int u,register int v)
{
    e[++cnt]=(edge){v,head[u]};
    head[u]=cnt;
}
int n,siz[N],sizs[N],fa[N],f[N],fac[N];
inline void dfs1(register int x)
{
    siz[x]=1;
    if(x!=1)
        sizs[x]=1;
    for(register int i=head[x];i;i=e[i].next)
    {
        int v=e[i].to;
        if(v==fa[x])
            continue;
        fa[v]=x;
        dfs1(v);
        siz[x]+=siz[v];
        ++sizs[x];
    }
}
inline void dfs2(register int x)
{
    f[x]=fac[sizs[x]];
    for(register int i=head[x];i;i=e[i].next)
    {
        int v=e[i].to;
        if(v==fa[x])
            continue;
        dfs2(v);
        f[x]=1ll*f[x]*f[v]%mod;
    }
}
int main()
{
    fac[0]=1;
    for(register int i=1;i<N;++i)
        fac[i]=1ll*i*fac[i-1]%mod;
    n=read();
    for(register int i=1;i<n;++i)
    {
        int u=read(),v=read();
        add(u,v),add(v,u);
    }
    dfs1(1);
    dfs2(1);
    write(1ll*f[1]*n%mod);
    return 0;
}

Guess you like

Origin www.cnblogs.com/yzhang-rp-inf/p/10994323.html