codeforces814E
https://lunch.blog.luogu.org/cf814e-an-unavoidable-detour-for-homedp-ji-shuo-post
https://blog.csdn.net/qq_31759205/article/details/77715991
An unavoidable detour for home
CodeForces - 814E
Those unwilling to return home from a long journey, will be affected by the oddity of the snail and lose their way. Mayoi, the oddity's carrier, wouldn't like this to happen, but there's nothing to do with this before a cure is figured out. For now, she would only like to know the enormous number of possibilities to be faced with if someone gets lost.
There are n towns in the region, numbered from 1 to n. The town numbered 1 is called the capital. The traffic network is formed by bidirectional roads connecting pairs of towns. No two roads connect the same pair of towns, and no road connects a town with itself. The time needed to travel through each of the roads is the same. Lost travelers will not be able to find out how the towns are connected, but the residents can help them by providing the following facts:
- Starting from each town other than the capital, the shortest path (i.e. the path passing through the minimum number of roads) to the capital exists, and is unique;
- Let li be the number of roads on the shortest path from town i to the capital, then li ≥ li - 1 holds for all 2 ≤ i ≤ n;
- For town i, the number of roads connected to it is denoted by di, which equals either 2 or 3.
You are to count the number of different ways in which the towns are connected, and give the answer modulo 109 + 7. Two ways of connecting towns are considered different if a pair (u, v) (1 ≤ u, v ≤ n) exists such there is a road between towns u and v in one of them but not in the other.
Input
The first line of input contains a positive integer n (3 ≤ n ≤ 50) — the number of towns.
The second line contains n space-separated integers d1, d2, ..., dn (2 ≤ di ≤ 3) — the number of roads connected to towns 1, 2, ..., n, respectively. It is guaranteed that the sum of di over all i is even.
Output
Output one integer — the total number of different possible ways in which the towns are connected, modulo 109 + 7.
Examples
4
3 2 3 2
1
5
2 3 3 2 2
2
5
2 2 2 2 2
2
20
2 2 2 2 3 2 3 2 2 2 2 2 2 2 2 2 2 3 3 2
82944
Note
In the first example, the following structure is the only one to satisfy the constraints, the distances from towns 2, 3, 4 to the capital are all 1.
In the second example, the following two structures satisfy the constraints.
/* g的状态转移: ①当i=j=k=0时,g[i][j][k]=1 ②当 j=0,k>0时,g[i][j][k]=∑g[i][j][k-1-l]*c[k-1][l]*(l!)/2 l∈[2,k-1] 表示从前k-1个度数能变成3的点中选l个点出来与最后一个点构成环,这l+1度数都变成3 ③当i=0,j>0或k>0时,g[i][j][k]=g[i][j-2][k]*(j-1)+g[i][j][k-1]*k 前一部分:表示从j-1个度数能变成2的点中选一个出来与最后一个点相连,这2个点的度数都变成2 后一部分:表示从k个度数能变成2的点中选一个出来与最后一个点相连,这个点变成度数3,最后一个点变成度数2 ④当i>0时,g[i][j][k]=g[i-1][j-1][k]*j+g[i-1][j+1][k-1]*k 前一部分:表示从j个度数能变成2的点中选一个出来与最后一个点相连,这个点变成度数2 后一部分:表示从k个度数能变成3的点钟选一个出来与最后一个点相连,这个点变成度数3 */ #include <bits/stdc++.h> using namespace std; typedef long long ll; inline ll read() { ll s=0; bool f=0; char ch=' '; while(!isdigit(ch)) {f|=(ch=='-'); ch=getchar();} while(isdigit(ch)) {s=(s<<3)+(s<<1)+(ch^48); ch=getchar();} return (f)?(-s):(s); } #define R(x) x=read() inline void write(ll x) { if(x<0) {putchar('-'); x=-x;} if(x<10) {putchar(x+'0'); return;} write(x/10); putchar((x%10)+'0'); } #define W(x) write(x),putchar(' ') #define Wl(x) write(x),putchar('\n') const int N=55; const ll Mod=1000000007; int n,deg[N]; ll fac[N],C[N][N]; ll ans=0,dp[N][N],g[N][N][N]; /* dp[i][j]表示前i个节点分为若干层,最后一层有j个节点的方案数 g[i][j][k]表示当前这层有i个节点,上一层有j个度数为2,k个度数为3的 */ inline void Ad(ll &x,ll y) { x+=y; x-=(x>=Mod)?Mod:0; } int main() { // freopen("data.in","r",stdin); // freopen("my.out","w",stdout); int i,j,k,l,c2,c3; R(n); for(i=1;i<=n;i++) R(deg[i]); fac[0]=fac[1]=0; fac[2]=1; for(i=3;i<=n;i++) fac[i]=fac[i-1]*i%Mod; //fac[i]=(i!)/2; C[0][0]=1; for(i=1;i<=n;i++) { C[i][0]=1; for(j=1;j<=n;j++) C[i][j]=(C[i-1][j-1]+C[i-1][j])%Mod; } g[0][0][0]=1;//i=0就是最下面的时候,要把最后一层连好的方案数 for(j=0;j<=n;j++) for(k=0;k<=n-j;k++) { if(j==0&&k>0) { for(l=2;l<=k-1;l++) Ad(g[0][j][k],g[0][j][k-l-1]*C[k-1][l]%Mod*fac[l]%Mod);//新加的点凑个环 } else { if(j>=2) Ad(g[0][j][k],g[0][j-2][k]*(j-1)%Mod);//度数为2的必须成对加 if(k>=1) Ad(g[0][j][k],g[0][j][k-1]*k%Mod); } } for(i=1;i<=n;i++) { for(j=0;j<=n-i;j++) for(k=0;k<=n-i-j;k++) { if(j>=1) Ad(g[i][j][k],g[i-1][j-1][k]*j%Mod); if(k>=1) Ad(g[i][j][k],g[i-1][j+1][k-1]*k%Mod); } } // for(i=0;i<=n;i++) for(j=0;j<=n;j++) for(k=0;k<=n;k++) // { // cout<<i<<' '<<j<<' '<<k<<' '<<g[i][j][k]<<endl; // } dp[deg[1]+1][deg[1]]=1; for(i=deg[1]+2;i<=n;i++) { for(j=1;j<=i-deg[1]-1;j++) //从第三层开始 { c2=0; c3=0; for(k=1;k<=i-j;k++)//枚举上一层有k个点 { if(deg[i-j-k+1]==2) c2++; else c3++; Ad(dp[i][j],dp[i-j][k]*g[j][c2][c3]%Mod); } } } c2=0; c3=0; for(i=1;i<n;i++) { if(deg[n-i+1]==2) c2++; else c3++; Ad(ans,dp[n][i]*g[0][c2][c3]%Mod); } Wl(ans); return 0; }