题解:P9346 无可奈何花落去

· · 题解

solution

考虑断边顺序不重要,只需考虑断边集合即可,顺序可以乘个阶乘啥的求出来。

那期望当然就是枚举权值,也就是天数,此时我们令 p_i 为恰好断掉 i 边后成若干条链的方案数,发现可能 p_i 中包含着恰好断掉 i - 1 条边就已经成若干条链的情况,所以容斥一下,p_i - p_{i - 1} 便是我们想要的结果。然后乘上排列组合的系数,那么答案就是 \frac{\sum_{i = 0}^{n - 1} (p_i \times ( n - 1 - i )! \times i! - p_{i - 1} \times (n - i)! \times (i - 1)!) \times i}{(n - 1)!}。

然后 p_i 很容易发现大概要写一个 DP 状物求出来,你可以选择将上述式子中排列组合的系数算到 DP 里去。那么链式 DP 比较常见的状态就是 f_{i, j, 0/1/2} 表示 i 的子树内断了 j 条边,然后向子树连了 0/1/2 条边的方案数。

转移的话就是一个树上背包,仔细分讨一下即可,这一点还是不难的。然后这东西的复杂度证明了按照子树大小 DP 是 O(n^2) 的。

那么 p_i = (f_{1, i, 0} + f_{1, i, 1} + f_{1, i, 2}),直接照算即可。

code

code:

#include <bits/stdc++.h>

using namespace std;

// #define int long long
#define fir first
#define sec second
#define mkp make_pair 
#define pb push_back
#define lep( i, l, r ) for ( int i = ( l ); i <= ( r ); ++ i )
#define rep( i, r, l ) for ( int i = ( r ); i >= ( l ); -- i )

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair < int, int > pii;

char _c; bool _f; template < class type > inline void read ( type &x ) {
    _f = 0, x = 0;
    while ( _c = getchar (), !isdigit ( _c ) ) if ( _c == '-' ) _f = 1;
    while ( isdigit ( _c ) ) x = x * 10 + _c - '0', _c = getchar (); if ( _f ) { x = -x; }
}

template < class type > inline void chkmin ( type &x, type y ) { x = ( x <= y ? x : y ); }
template < class type > inline void chkmax ( type &x, type y ) { x = ( x >= y ? x : y ); }

const int N = 5005;
const int mod = 985661441;

int n;
int f[N][N][3], siz[N], p[N];
ll g[N][3], fac[N];

int head[N], tot;

struct Node {
  int to, next;
} edges[N << 1];

void add ( int u, int v ) {
  tot ++;
  edges[tot].to = v;
  edges[tot].next = head[u];
  head[u] = tot;
}

ll fast_pow ( ll a, int b ) {
  ll res = 1;
  while ( b ) {
    if ( b & 1 ) {
      res = res * a;
      res %= mod;
    }
    b >>= 1;
    a = a * a;
    a %= mod;
  }
  return res;
}

void dfs ( int x ) {
  siz[x] = 1;
  f[x][0][0] = 1;
  for ( int i = head[x]; i; i = edges[i].next ) {
    dfs ( edges[i].to );
    for ( int j = 0; j <= siz[x] + siz[edges[i].to]; j ++ ) {
      g[j][0] = g[j][1] = g[j][2] = 0;
    }
    for ( int j = 0; j <= siz[x]; j ++ ) {
      for ( int k = 0; k <= siz[edges[i].to]; k ++ ) {
        g[j + k + 1][0] += 1ll * f[x][j][0] * ( ( ll ) f[edges[i].to][k][0] + f[edges[i].to][k][1] + f[edges[i].to][k][2] ) % mod;
        g[j + k + 1][0] %= mod;
        g[j + k + 1][1] += 1ll * f[x][j][1] * ( ( ll ) f[edges[i].to][k][0] + f[edges[i].to][k][1] + f[edges[i].to][k][2] ) % mod;
        g[j + k + 1][1] %= mod;
        g[j + k + 1][2] += 1ll * f[x][j][2] * ( ( ll ) f[edges[i].to][k][0] + f[edges[i].to][k][1] + f[edges[i].to][k][2] ) % mod;
        g[j + k + 1][2] %= mod;
        g[j + k][1] += 1ll * f[x][j][0] * ( f[edges[i].to][k][0] + f[edges[i].to][k][1] ) % mod;
        g[j + k][1] %= mod;
        g[j + k][2] += 1ll * f[x][j][1] * ( f[edges[i].to][k][0] + f[edges[i].to][k][1] ) % mod;
        g[j + k][2] %= mod;
      }
    }
    for ( int j = 0; j < siz[x] + siz[edges[i].to]; j ++ ) {
      f[x][j][0] = g[j][0], f[x][j][1] = g[j][1], f[x][j][2] = g[j][2];
    }
    siz[x] += siz[edges[i].to];
  }
}

void Solve () {
  cin >> n;
  for ( int i = 2; i <= n; i ++ ) {
    int fa;
    cin >> fa;
    add ( fa, i );
  }
  fac[0] = 1;
  for ( int i = 1; i <= n; i ++ ) {
    fac[i] = fac[i - 1] * i % mod;
  }
  dfs ( 1 );
  ll ans = 0;
  for ( int i = 0; i < n; i ++ ) {
    ll val = 1ll * ( ( ll ) f[1][i][0] + f[1][i][1] + f[1][i][2] ) % mod * fac[n - 1 - i] % mod * fac[i] % mod;
    p[i] = val;
    ans += 1ll * ( p[i] - p[i - 1] ) * i % mod;
    ans %= mod;
  }
  cout << ( 1ll * ans * fast_pow ( fac[n - 1], mod - 2 ) % mod + mod ) % mod;
}

signed main () {
#ifdef judge
    freopen ( "Code.in", "r", stdin );
    freopen ( "Code.out", "w", stdout );
    freopen ( "Code.err", "w", stderr );
#endif
    Solve ();
    return 0;
}