2020-2021 “Orz Panda” Cup Programming Contest D.Data Structure Master and Orz Pandas

题目描述

https://codeforces.com/gym/102870/problem/D

Solution

$\lim_{m\rightarrow\infty} \frac{f(m)}{m}$ 这个东西我们可以理解为在这颗树的形态(指轻重儿子)随机的情况下,一次操作的将轻儿子变成重儿子的期望次数

我们首先考虑一个点 $u$ 是轻儿子的概率,$p=1-\frac{sz[u]}{sz[fa]-1}$,那么一个点的期望就是从它到根的所有点是轻儿子的概率之和

证明坑

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
#include <iostream>
#include <cstdio>
#include <set>
#include <queue>
#define maxn 100010
#define ll long long
#define cn const node
using namespace std;

const int p = 998244353;

ll pow_mod(ll x, ll n) {
ll s = 1;
for (; n; n >>= 1) {
if (n & 1) s = s * x % p;
x = x * x % p;
}
return s;
}

int n, m;

struct Edge {
int to, next;
} e[maxn]; int c1, head[maxn];
inline void add_edge(int u, int v) {
e[c1].to = v; e[c1].next = head[u]; head[u] = c1++;
}

int sz[maxn]; ll dis[maxn];
void dfs(int u, int fa) {
sz[u] = 1;
for (int i = head[u]; ~i; i = e[i].next) {
int v = e[i].to; if (v == fa) continue;
dfs(v, u); sz[u] += sz[v];
}
}

ll ans;
void Dfs(int u, int fa) {
ans = (ans + dis[u]) % p;
for (int i = head[u]; ~i; i = e[i].next) {
int v = e[i].to; if (v == fa) continue;
dis[v] = (1 - sz[v] * pow_mod(sz[u] - 1, p - 2) + dis[u]) % p;
Dfs(v, u);
}
}

int main() { fill(head, head + maxn, -1);
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);

cin >> n;
for (int i = 2; i <= n; ++i) {
int x; cin >> x;
add_edge(x, i);
} dfs(1, 0); Dfs(1, 0);
cout << (ans * pow_mod(n, p - 2) % p + p) % p << "\n";
return 0;
}