题解:CF1097G Vladislav and a Great Legend

· · 题解

CF1097G Vladislav and a Great Legend

题意:给定一颗 n 个节点的树 T,对应任意非空顶点集 X,f(X) 定义为它在原图的极小连通子图中的边数,求:

\sum_{\varnothing \ne X \subseteq \{1, 2,\cdots, n\}} f(X)^k

其中 n \le 10^5,\ k \le 200,极小连通子图可认为是保留所有边的虚树。

\begin{aligned} \sum_{\varnothing \ne X \subseteq \{1, 2,\cdots, n\}} f(X)^k = \sum_{i = 0}^{k}\begin{Bmatrix}k\\i\end{Bmatrix}i! \sum_{X} \begin{pmatrix}f(X)\\i\end{pmatrix} \end{aligned} 枚举虚树根 $x$ 统计贡献。现在加入一颗子树 $v$。 肯定是 $v$ 里的点集与已经访问过子树中的点集(红色)进行**配对**,考虑一对点集产生多少方案。 ![](https://cdn.luogu.com.cn/upload/image_hosting/z2qcxfiu.png) 我们需要考虑 $1$ 选不选,$2$ 选不选,$3$ 选不选。 虽然他们都不是红色子树中的边,但是与 $v$ 中点集配对后都会被经过。 设 $f_{x, i}$ 表示以 $x$ 为根的子树内选中 $i$ 条边的生成树个数(**方案不一定合法**)。 注意这里**选中的边**不一定是生成树内部边,也有可能是生成树的根到 $x$ 之间的边。 现在考虑 $v$ 子树中的一棵生成树。 生成树内部以及生成树的根到 $v$ 之间的边都已经决策过了,只要决策 $(x, v)$ 选还是不选。 $$ f_{x, i + j}^{\prime} \gets f_{x, i} \times (f_{v, j} + f_{v, j - 1}) $$ 其中 $f^\prime$ 表示加入 $v$ 后的 $f$ 值。 注意这一部分的答案是要计入最终贡献的,因为统计的都是合法方案。 假设 $v$ 与右边结合的方案已经计算完了。 现在要把一些只在 $v$ 子树的点集加入 $f$,为下一棵子树的转移做准备。 点集的根到 $v$ 都决策过了,还是考虑选不选 $(x, y)$。 $$ f_{x, i}^{\prime} \gets f_{v, i} + f_{v, i - 1} $$ 复杂度据说转移时对 $k$ 和子树大小取 $\min$ 就能做到 $O(nk)$,很神秘。 ```cpp #include<bits/stdc++.h> #define eb emplace_back #define ep emplace using namespace std; using ll = long long; constexpr int N = 1e5 + 5, P = 1e9 + 7; inline void inc(ll &x, ll y) {(x += y) %= P;} int n, k, sz[N]; vector<int> g[N]; ll f[N][205], tmp[205], a[205], s[205][205], fac = 1, ans; void dfs(int x, int fa) { f[x][0] = sz[x] = 1; for(int y : g[x]) { if(y == fa) continue; dfs(y, x); for(int i = 0; i <= k; ++ i) tmp[i] = f[x][i]; for(int i = 0; i <= min(sz[x], k); ++ i) { for(int j = 0; j <= min(k - i, sz[y]); ++ j) { int val = tmp[i] * f[y][j] % P; inc(f[x][i + j], val), inc(a[i + j], val); if(i + j < k) inc(f[x][i + j + 1], val), inc(a[i + j + 1], val); } } sz[x] += sz[y]; inc(f[x][0], f[y][0]); for(int i = 1; i <= k; ++ i) inc(f[x][i], f[y][i] + f[y][i - 1]); } } int main() { cin.tie(0)->sync_with_stdio(0); cin >> n >> k; s[0][0] = 1; for(int i = 1; i <= k; ++ i) { for(int j = 1; j <= i; ++ j) { inc(s[i][j], s[i - 1][j] * j + s[i - 1][j - 1]); } } for(int i = 1; i <= n - 1; ++ i) { int x, y; cin >> x >> y; g[x].eb(y); g[y].eb(x); } dfs(1, 0); for(int i = 1; i <= k; ++ i) { fac = fac * i % P; inc(ans, fac * s[k][i] % P * a[i]); } cout << ans << '\n'; return 0; } ```