1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| #include<bits/stdc++.h> using namespace std;
typedef long long ll; typedef unsigned long long ull; typedef double db; typedef pair <int,int> Pii; #define reg register #define mp make_pair #define pb push_back #define Mod1(x) ((x>=P)&&(x-=P)) #define Mod2(x) ((x<0)&&(x+=P)) #define rep(i,a,b) for(int i=a,i##end=b;i<=i##end;++i) #define drep(i,a,b) for(int i=a,i##end=b;i>=i##end;--i) template <class T> inline void cmin(T &a,T b){ ((a>b)&&(a=b)); } template <class T> inline void cmax(T &a,T b){ ((a<b)&&(a=b)); }
char IO; template <class T=int> T rd(){ T s=0; int f=0; while(!isdigit(IO=getchar())) f|=IO=='-'; do s=(s<<1)+(s<<3)+(IO^'0'); while(isdigit(IO=getchar())); return f?-s:s; }
const int N=5e3+10,P=998244353;
int n,m; int I[N],J[N],C[N][N]; ll qpow(ll x,ll k=P-2) { ll res=1; for(;k;k>>=1,x=x*x%P) if(k&1) res=res*x%P; return res; }
struct Edge{ int to,nxt; } e[N<<1]; int head[N],ecnt; void AddEdge(int u,int v){ e[++ecnt]=(Edge){v,head[u]}; head[u]=ecnt; } int dp[N][N][2]; int G[N][3],H[N][3],sz[N];
void dfs(int u,int f){ sz[u]=0; for(int i=head[u];i;i=e[i].nxt){ int v=e[i].to; if(v==f) continue; dfs(v,u); } G[0][0]=1,G[0][1]=G[0][2]=0; for(int i=head[u];i;i=e[i].nxt){ int v=e[i].to; if(v==f) continue; rep(i,0,sz[u]+sz[v]) rep(j,0,2) H[i][j]=G[i][j],G[i][j]=0; rep(i,0,sz[u]) rep(a,0,2) if(H[i][a]) rep(j,0,sz[v]) rep(b,0,min(1,2-a)) G[i+j][a+b]=(G[i+j][a+b]+1ll*H[i][a]*dp[v][j][b])%P; sz[u]+=sz[v]; } rep(i,0,sz[u]+1) dp[u][i][0]=dp[u][i][1]=0; rep(i,0,sz[u]) { dp[u][i+1][0]=(0ll+dp[u][i+1][0]+G[i][0]+2*G[i][1]+2*G[i][2])%P; dp[u][i][1]=(0ll+dp[u][i][1]+G[i][0]+G[i][1])%P; } sz[u]++; }
int F[N],T[N]; void Get(){ n=rd(); rep(i,1,n) head[i]=0; ecnt=0; rep(i,2,n) { int u=rd(),v=rd(); AddEdge(u,v),AddEdge(v,u); } dfs(1,0); rep(i,1,n) { F[i]=dp[1][i][0],T[i]=0; ll t=1ll*F[i]*J[i]%P; rep(j,1,i) { T[j]=(T[j]+((i-j)&1?P-1:1)*t%P*C[i-1][i-j]%P*I[j])%P; } } }
int S[N],c;
int main(){ rep(i,J[0]=1,N-1) J[i]=1ll*J[i-1]*i%P; I[N-1]=qpow(J[N-1]); drep(i,N-1,1) I[i-1]=1ll*I[i]*i%P; rep(i,0,N-1) rep(j,C[i][0]=1,i) C[i][j]=C[i-1][j]+C[i-1][j-1],Mod1(C[i][j]); m=rd(); if(m==1) return n=rd(),printf("%d\n",n<=2),0; S[0]=1; rep(t,1,m-1) { Get(); drep(i,n+c,0) { S[i]=0; rep(j,1,min(i,n)) S[i]=(S[i]+1ll*S[i-j]*T[j])%P; } c+=n; } Get(); rep(i,1,n) { F[i]=dp[1][i][0],T[i]=0; ll t=1ll*F[i]*J[i-1]%P; rep(j,1,i) T[j]=(T[j]+((i-j)&1?P-1:1)*t%P*C[i-1][i-j]%P*I[j-1])%P; } int ans=0; rep(i,1,c) if(S[i]) rep(j,1,n) if(T[j]) { int t=1ll*J[i]*J[j-1]%P*C[i-1+j-1][j-1]%P; ans=(ans+1ll*t*S[i]%P*T[j])%P; } printf("%d\n",ans); }
|