题意
给你一个仙人掌,随机选一个点排列。按照这个排列的顺序将所有点删掉,求每个点被删掉时连通块大小之和的期望。
n
≤
400
n\leq 400
n≤400
分析
- 树上一条路径可行的概率是1/len.
- 环上做一个容斥:路径A可行+路径B可行-路径AB均可行。
- 仙人掌上做dp容斥即可。
仙人掌上有几个处理的技巧:
- 仙人掌不需要tarjan. 暴力建出所有点向环顶连边的树。
- 做dp的时候,所有边可以被分为三类:树边,儿子向环顶,环顶向儿子。
- 对后两类做一个特殊讨论即可。
O ( n 3 ) O(n^3) O(n3)
#include <bits/stdc++.h>
using namespace std;
const int mo = 998244353, N = 410;
typedef long long ll;
int n, m;
ll jc[N], njc[N], inv[N];
ll ksm(ll x, ll y) {
ll ret = 1;
for(; y; y >>= 1) {
if (y & 1) ret = ret * x % mo;
x = x * x % mo;
}
return ret;
}
int len[N];
ll f[N][N];
int no[N], cir[N], cnt, cirbel[N];
int final[N], nex[N * 8], to[N * 8], tot = 1;
void link(int x, int y) {
to[++tot] = y, nex[tot] = final[x], final[x] = tot;
}
int dfn[N], stm, p[N], e[N * 8]; //p是指向父亲的边
int cirfa[N], dep[N];
void dfs(int x) {
dfn[x] = ++ stm;
for(int i = final[x]; i; i = nex[i]) {
if (i == p[x]) continue;
int y = to[i];
if (dfn[y] == 0) {
p[y] = i ^ 1;
dep[y] = dep[x] + 1;
dfs(y);
} else if(dfn[y] < dfn[x]) {
e[i] = e[i ^ 1] = 1;
++cnt;
for(int z = x, w = 1; z != y; z = to[p[z]], w++) {
no[z] = w;
len[z] = dep[x] - dep[y] + 1;
link(z, y), link(y, z);
e[p[z]] = e[p[z] ^ 1] = 1;
cirfa[z] = y;
cirbel[z] = cnt;
}
}
}
}
ll ans;
void go(int x, int from) {
for(int i = 1; i <= n; i ++) {
ans = (ans + inv[i] * f[x][i]) % mo;
}
for(int i = final[x]; i; i = nex[i]) if (e[i] == 0) {
int y = to[i];
if (y == from) continue;
if (y != cirfa[x] && cirfa[y] != x) { //树边
for(int i = 1; i <= n; i++) f[y][i] = f[x][i - 1];
go(y, x);
} else {
int ori = 0, a = 0, b = 0, cirlen = 0;
if (cirfa[x] == y) {
ori = x, a = no[x], b = len[x] - no[x], cirlen = len[x];
} else {
if (cirbel[y] == cirbel[from]) {
cirlen = len[y];
ori = from, a = (no[from] - no[y] + len[y]) % cirlen, b = cirlen - a;
} else {
ori = x, a = no[y], b = len[y] - no[y], cirlen = len[y];
}
}
for(int i = 1; i <= n; i++) {
if (i - a > 0) f[y][i] = f[ori][i - a];
if (i - b > 0) f[y][i] = (f[y][i] + f[ori][i - b]) % mo;
if (i - cirlen + 1 > 0) f[y][i] = (f[y][i] - f[ori][i - cirlen + 1]) % mo;
}
go(y, x);
}
}
}
int main() {
freopen("falldream.in","r",stdin);
// freopen("falldream.out","w",stdout);
cin >> n >> n >> m;
for(int i = 1; i <= m; i++) {
int x, y; scanf("%d %d", &x, &y);
link(x, y), link(y, x);
}
dfs(1);
jc[0] = 1;
for(int i = 1; i <= n; i++) jc[i] = jc[i - 1] * i % mo;
njc[n] = ksm(jc[n], mo - 2);
for(int i = n - 1; ~i; i--) njc[i] = njc[i + 1] * (i + 1) % mo;
for(int i = 1; i <= n; i++) inv[i] = njc[i] * jc[i - 1] % mo;
for(int r = 1; r <= n; r++) {
memset(f,0,sizeof f);
f[r][1] = 1;
go(r, 0);
}
cout << (ans + mo) % mo << endl;
}


被折叠的 条评论
为什么被折叠?



