//Place your snippets for cpp here. Each snippet is defined under a snippet name and has a prefix, body and 
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
// same ids are connected.
// prefix 的格式要求是 acm-[一级标题编号].[二级标题编号][该代码片段的英文简称]
// description 的格式要求是 "插入 [该代码片段的中文名]"

{
    "树上背包优化O复杂度 (NOI Online 2 游戏)": {
        "prefix": "acm-4.5nk",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=5003,P=998244353;",
            "int n,m,o,x,y,f[N],g[N],A[N],jc[N],head[N];char s[N];",
            "struct QAQ{int to,next;}a[N<<1];",
            "inline void add(Re x,Re y){a[++o].to=y,a[o].next=head[x],head[x]=o;}",
            "inline void in(Re &x){",
            "    int f=0;x=0;char c=getchar();",
            "    while(c<'0'||c>'9')f|=c=='-',c=getchar();",
            "    while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();",
            "    x=f?-x:x;",
            "}",
            "inline int mi(Re x,Re k){",
            "\tRe s=1;",
            "\twhile(k){",
            "\t\tif(k&1)s=(LL)s*x%P;",
            "\t\tx=(LL)x*x%P,k>>=1;",
            "\t}",
            "\treturn s;",
            "}",
            "inline int inv(Re x){return mi(x,P-2);}",
            "int sz1[N],sz0[N],lim[N],dp[N][N],tmp[N];",
            "inline void dfs0(Re x,Re fa){",
            "\tsz0[x]=(!A[x]),sz1[x]=A[x];",
            "\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\tif((to=a[i].to)!=fa)dfs0(to,x),sz1[x]+=sz1[to],sz0[x]+=sz0[to];",
            "//\tlim[x]=min(sz1[x],size[x]-sz1[x]);",
            "}",
            "inline void dfs(Re x,Re fa){",
            "\tdp[x][0]=1;",
            "\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\tif((to=a[i].to)!=fa){",
            "\t\t\tdfs(to,x);",
            "\t\t\tfor(Re j=0;j<=lim[x]+lim[to];++j)tmp[j]=0;",
            "\t\t\tfor(Re j=lim[x];j>=0;--j)",
            "\t\t\t\tfor(Re k=lim[to];k>=0;--k)",
            "\t\t\t\t\t(tmp[j+k]+=(LL)dp[x][j]*dp[to][k]%P)%=P;",
            "\t\t\tlim[x]+=lim[to];",
            "\t\t\tfor(Re j=0;j<=lim[x];++j)dp[x][j]=tmp[j];",
            "\t\t}",
            "\t++lim[x];",
            "\tif(A[x])for(Re j=min(lim[x],sz0[x]);j>=1;--j)",
            "\t\t(dp[x][j]+=(LL)dp[x][j-1]*(sz0[x]-(j-1))%P)%=P;",
            "\telse for(Re j=min(lim[x],sz1[x]);j>=1;--j)",
            "\t\t(dp[x][j]+=(LL)dp[x][j-1]*(sz1[x]-(j-1))%P)%=P;",
            "\twhile(!dp[x][lim[x]])--lim[x];",
            "}",
            "inline int C(Re m,Re n){return (LL)jc[n]*inv(jc[m])%P*inv(jc[n-m])%P;}",
            "int main(){",
            "//\tfreopen(\"match.in\",\"r\",stdin);",
            "//\tfreopen(\"match.out\",\"w\",stdout);",
            "\tin(n),scanf(\"%s\",s+1),m=n-1,jc[0]=1;",
            "\tfor(Re i=1;i<=n;++i)A[i]=s[i]-'0',jc[i]=(LL)jc[i-1]*i%P;",
            "\twhile(m--)in(x),in(y),add(x,y),add(y,x);",
            "\tdfs0(1,0),dfs(1,0);",
            "//\tfor(Re i=1;i<=n;++i)printf(\"lim[%d]=%d\\n\",i,lim[i]);",
            "//\tfor(Re i=0;i<=n/2;++i)printf(\"dp[%d]=%d\\n\",i,dp[1][i]);",
            "//\tfor(Re j=0;j<=lim[1];++j)printf(\"%d \",dp[1][j]);",
            "\tfor(Re j=0;j<=lim[1];++j)g[j]=(LL)dp[1][j]*jc[n/2-j]%P;",
            "\tfor(Re i=lim[1];i>=0;--i){",
            "\t\tf[i]=g[i];",
            "\t\tfor(Re j=i+1;j<=lim[1];++j)f[i]=(f[i]-(LL)C(i,j)*f[j]%P+P)%P;",
            "\t}",
            "\tfor(Re i=0;i<=n/2;++i)printf(\"%d\\n\",f[i]);",
            "\tfclose(stdin);",
            "\tfclose(stdout);",
            "\treturn 0;",
            "}",
            ""
        ],
        "description": "插入 树上背包优化O复杂度 (NOI Online 2 游戏)"
    }
}