0% found this document useful (0 votes)
3 views2 pages

code-text for a hard problem

The document is a C++ program that implements a solution to a combinatorial problem using depth-first search (DFS) and dynamic programming. It defines functions for reading input, calculating combinations, and performing DFS on a tree structure to compute values stored in multi-dimensional arrays. The program processes multiple test cases and outputs results based on the computed values.

Uploaded by

hereisutkarsh01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

code-text for a hard problem

The document is a C++ program that implements a solution to a combinatorial problem using depth-first search (DFS) and dynamic programming. It defines functions for reading input, calculating combinations, and performing DFS on a tree structure to compute values stored in multi-dimensional arrays. The program processes multiple test cases and outputs results based on the computed values.

Uploaded by

hereisutkarsh01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1 #include<bits/stdc++.

h>
2 using namespace std;
3 typedef long long ll;
4 int read() {
5 int x=0,f=1;
6 char c=getchar();
7 while(c<'0'||c>'9') {
8 if(c=='-')f=-1;
9 c=getchar();
10 }
11 while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();
12 return x*f;
13 }
14 namespace tokido_saya {
15 const int maxn=81,mod=998244353;
16 typedef vector<int>::iterator iter;
17 int n,lp[maxn],rp[maxn],siz[maxn],t;
18 vector<int> v[maxn];
19 ll f[maxn][maxn][maxn][maxn],dp[maxn][maxn],fac[maxn],inv[maxn];
20 int zc[maxn][maxn][maxn][maxn];
21 ll qpow(ll x,int y)
22 {
23 ll w=1;
24 while(y)
25 {
26 if(y&1)w=w*x%mod;
27 x=x*x%mod,y>>=1;
28 }
29 return w;
30 }
31 ll C(int x,int y)
32 {
33 if(y<0||y>x)return 0;
34 return fac[x]*inv[x-y]%mod*inv[y]%mod;
35 }
36 void dfs1(int u)
37 {
38 lp[u]=rp[u]=u;
39 for(iter
it=v[u].begin();it!=v[u].end();it++)dfs1(*it),rp[u]=max(rp[u],rp[*it]);
40 }
41 void dfs2(int u)
42 {
43 f[u][0][0][0]=1,dp[u][0]=1;
44 for(iter it=v[u].begin();it!=v[u].end();it++)
45 {
46 int v=*it;
47 dfs2(v);
48 for(int i=siz[u];i>=0;i--)
49 for(int
j=siz[v];j;j--)dp[u][i+j]=(dp[u][i+j]+dp[u][i]*dp[v][j]%mod*C(i+j,j))%
mod;
50 for(int j1=0;j1<=siz[v];j1++)
51 for(int k1=0;k1<=min(j1+1,siz[v]);k1++)
52 {
53 memset(zc[j1][k1],0,sizeof(zc[j1][k1]));
54 for(int j=0;j<=siz[u];j++)
55 for(int k=0;k<=j+1;k++)
56 for(int
w=0;w<=min(k1,j);w++)zc[j1][k1][j+j1-w][k+k1-w]=(zc[j1][k1
][j+j1-w][k+k1-w]+f[u][0][j][k]*C(j,w)%mod*C(k+k1-w,k))%mo
d;
57 }
58 memset(f[u][0],0,sizeof(f[u][0]));
59 for(int j=0;j<=siz[v];j++)
60 for(int k=0;k<=j+1;k++)
61 for(int j1=0;j1<=siz[u]+siz[v];j1++)
62 for(int k1=0;k1<=min(siz[u]+siz[v],j1+1);k1++)
63 if(zc[j][k][j1][k1])
64 {
65 const int w=zc[j][k][j1][k1];
66 f[u][0][j1][k1]=(f[u][0][j1][k1]+f[v][0][j][k]*w)%mod;
67 for(int
i=lp[v];i<=rp[v];i++)f[u][i][j1][k1]=(f[u][i][j1][k1]+
f[v][i][j][k]*w)%mod;
68 }
69 for(int i=rp[v]+1;i<=rp[u];i++)
70 for(int j=siz[u]-1;j>=i-rp[v]-1;j--)
71 for(int k=j+1;k>=0;k--)
72 {
73 const int val=f[u][i][j][k];
74 f[u][i][j][k]=0;
75 for(int p=0;p<=siz[v];p++)
76 for(int
w=0;w<=min(p,j-(i-rp[v]-1));w++)f[u][i][j+siz[v]-w][k+p-w]
=(f[u][i][j+siz[v]-w][k+p-w]+dp[v][p]*val%mod*C(j-(i-rp[v]
-1),w)%mod*C(k+p-w,k))%mod;
77 }
78 siz[u]+=siz[v];
79 }
80 for(int i=siz[u];i>=0;i--)dp[u][i+1]=(dp[u][i+1]+dp[u][i])%mod;
81 if(u!=1)for(int j=siz[u];j>=0;j--)
82 for(int k=min(j+1,siz[u]);k>=0;k--)
83 {
84 ll sum=0;
85 for(int i=rp[u];i>=lp[u];i--)
86 {
87 const int w=f[u][i][j][k];
88 f[u][i][j+1][k]=(f[u][i][j+1][k]+w)%mod;
89 if(j==siz[u]-1)f[u][i][j+1][k+1]=(f[u][i][j+1][k+1]+w)%mod;
90 f[u][i][j][k]=sum,sum=(sum+w)%mod;
91 }
92 const int w=f[u][0][j][k];
93
f[u][lp[u]][j][k]=(f[u][lp[u]][j][k]+w)%mod,f[u][0][j+1][k]=(f[u][0][j
+1][k]+w+sum)%mod;
94
if(j==siz[u])f[u][0][j+1][k+1]=(f[u][0][j+1][k+1]+w)%mod,f[u][lp[u]][j
][k+1]=(f[u][lp[u]][j][k+1]+w)%mod;
95 }
96 siz[u]++;
97 }
98 int main() {
99 int x,y;
100 t=read(),fac[0]=1;
101 for(int i=1;i<=80;i++)fac[i]=fac[i-1]*i%mod;
102 inv[80]=qpow(fac[80],mod-2);
103 for(int i=80;i;i--)inv[i-1]=inv[i]*i%mod;
104 while(t--)
105 {
106
n=read(),memset(f,0,sizeof(f)),memset(dp,0,sizeof(dp)),memset(siz,0,sizeof
(siz));
107 for(int i=1;i<=n;i++)v[i].clear();
108 for(int i=1;i<n;i++)
109 {
110 x=read(),y=read();
111 if(x>y)swap(x,y);
112 v[x].push_back(y);
113 }
114 for(int
i=1;i<n;i++)sort(v[i].begin(),v[i].end()),reverse(v[i].begin(),v[i].end())
;
115 dfs1(1),dfs2(1);
116 for(int i=n;i>1;i--)printf("%lld ",f[1][i][i-2][0]);
117 puts("1");
118 }
119 return 0;
120 }
121 }
122 int main() {
123 return tokido_saya::main();
124 }

You might also like