CodeForces 1097G. Vladislav and a Great Legend
题目简述:给定$n \leq 10^5$个节点的树$T = (V, E)$,令$X \subseteq V$表示一个非空节点集合,定义$f(X)$为包含$X$的最小子树的边数。求
$$ \sum_{\emptyset \neq X \subseteq V} (f(X))^k, $$
其中$k \leq 200$。
解:code
问题转化
我们额外定义$f(\emptyset) = 0$,就不需再单独考虑空集。
利用斯特林数的性质,我们有
$$ x^n = \sum_{k=0}^n k! \begin{Bmatrix} n \\ k \end{Bmatrix} \binom{x}{k}. $$
于是,
$$ \sum_{X \subseteq V} (f(X))^k = \sum_{i=0}^k i! \begin{Bmatrix} n \\ k \end{Bmatrix} \sum_{X \subseteq V} \binom{f(X)}{i}. $$
注意到
$$ \sum_{X \subseteq V} \binom{f(X)}{k} = \sum_{X \subseteq V} \sum_{|Y| = k} [Y \subseteq T(X)], $$
其中$T(X)$表示包含$X$的最小子树的边集。
最小子树边集的刻画
我们考虑$X$中所有节点的最近公共祖先是$x$的情况,即$\text{LCA}(X) = x$,则包含$X$的最小子树的边集$T(X)$可被刻画成:若一个节点$u \neq x$,以其为根的子树$T_u$中存在$X$的一个节点,即$T_u \cap X \neq \emptyset$,则$u$与其父节点$\text{pre}(u)$的边$(u, \text{pre}(u))$必定在最小子树中。形式化地,若$\text{LCA}(X) = x$,则
$$ T(X) = \{ (u, \text{pre}(u)): u \in T_x \setminus \{x\} \land X \cap T_u \neq \emptyset \}. $$
广义最小子树边集
在上述讨论中,我们在假设了$\text{LCA}(X) = x$的条件下,得到$T(X)$的刻画。我们现在去掉$\text{LCA}(X) = x$的限制条件,直接对每个节点$x \in V$,定义
$$ F_x(X) = \{ (u, \text{pre}(u)): u \in T_x \setminus \{x\} \land X \cap T_u \neq \emptyset \}. $$
类似地,我们定义
$$ G_x(X) = \{ (u, \text{pre}(u)): u \in T_x \land X \cap T_u \neq \emptyset \}. $$
我们观察到以下两个性质:
观察0:$F_x(\emptyset) = G_x(\emptyset) = \emptyset$。
观察1:若$X \neq \emptyset$,则$G_x(X) = F_x(X) \cup \{ (x, \text{pre}(x)) \}$。
观察2:若$\text{LCA}(X) = x$,则$T(X) = F_x(X)$。
观察3:设$y$是$x$的子节点,即$y \in \text{son}(x)$,则$Y \subseteq G_y(X)$当且仅当$Y \subseteq F_x(X)$且$\text{LCA}(X) \in T_y$。
再次问题转化
我们令$f[x][k]$表示以$x$为根的子树$T_x$中节点的所有子集$X$的广义最小子树$F_x(X)$中选择$k$条边的方案数之和,即
$$ f[x][k] = \sum_{X \subseteq T_x} \sum_{|Y| = k} [Y \subseteq F_x(X)]. $$
令$g[x][k]$表示以$x$为根的子树$T_x$中节点的所有子集$X$最小子树$G_x(X)$中选择$k$条边的方案数之和,即
$$ g[x][k] = \sum_{X \subseteq T_x} \sum_{|Y| = k} [Y \subseteq G_x(X)]. $$
我们枚举$X$的最近公共祖先$\text{LCA}(X)$,则
$$
\begin{aligned}
\sum_{X \subseteq V} \binom{f(X)}{k}
& = \sum_{x \in V} \sum_{X \subseteq V} [\text{LCA}(X) = x] \sum_{|Y| = k} [Y \subseteq T(X)] \\
& = \sum_{x \in V} \sum_{|Y| = k} \sum_{X \subseteq T_x} [\text{LCA}(X) = x \land Y \subseteq T(X)] \\
& = \sum_{x \in V} \sum_{|Y| = k} \sum_{X \subseteq T_x} [\text{LCA}(X) = x \land Y \subseteq F_x(X)] \\
& = \sum_{x \in V} \sum_{|Y| = k} \sum_{X \subseteq T_x} \Big( [Y \subseteq F_x(X)] - [Y \subseteq F_x(X) \land \text{LCA}(X) \neq x] \Big) \\
& = \sum_{x \in V} \sum_{|Y| = k} \sum_{X \subseteq T_x} \left( [Y \subseteq F_x(X)] - \sum_{y \in \text{son}(x)} [Y \subseteq F_x(X) \land \text{LCA}(X) \in T_y] \right) \\
& = \sum_{x \in V} \sum_{|Y| = k} \sum_{X \subseteq T_x} \left( [Y \subseteq F_x(X)] - \sum_{y \in \text{son}(x)} [Y \subseteq G_x(X)] \right) \\
& = \sum_{x \in V} \left( \sum_{|Y| = k} \sum_{X \subseteq T_x} [Y \subseteq F_x(X)] - \sum_{y \in \text{son}(x)} \sum_{|Y| = k} \sum_{X \subseteq T_x} [Y \subseteq G_x(X)] \right) \\
& = \sum_{x \in V} \left( f[x][k] - \sum_{y \in \text{son}(x)} g[y][k] \right) \\
\end{aligned}
$$
动态规划
以上问题转化后,剩下的问题变成了求所有$f[x][k]$和$g[x][k]$。
注意到
$$
\begin{aligned}
g[x][k]
& = \sum_{X \subseteq T_x} \sum_{|Y| = k} [Y \subseteq G_x(X)] \\
& = \sum_{X \subseteq T_x} \sum_{|Y| = k} \Big( [Y \subseteq G_x(X) \land (x, \text{pre}(x)) \in Y] + [Y \subseteq G_x(X) \land (x, \text{pre}(x)) \notin Y] \Big) \\
& = \sum_{X \subseteq T_x} \left( [G_x(X) \neq \emptyset] \sum_{|Y| = k-1} [Y \subseteq F_x(X)] + \sum_{|Y|=k} [Y \subseteq F_x(X)] \right) \\
& = \sum_{X \subseteq T_x} \left( \sum_{|Y| = k-1} [Y \subseteq F_x(X)] + \sum_{|Y|=k} [Y \subseteq F_x(X)] \right) - \sum_{X \subseteq T_x} [G_x = \emptyset] \sum_{|Y| = k-1} [Y \subseteq F_x(X)] \\
& = f[x][k-1]+f[x][k]-[k = 1].
\end{aligned}
$$
设$\text{son}(x) = \{ y_1, y_2, \dots, y_m \}$,则
$$
\begin{aligned}
f[x][k]
& = \sum_{X \subseteq T_x} \sum_{|Y| = k} [Y \subseteq F_x(X)] \\
& = \sum_{X \subseteq T_x} \sum_{|Y| = k} \left( [x \in X \land Y \subseteq F_x(X)] + [x \notin X \land Y \subseteq F_x(X)] \right) \\
& = 2 \sum_{X \subseteq T_x} \sum_{|Y| = k} [x \notin X \land Y \subseteq F_x(X)] \\
& = 2 \sum_{X \subseteq T_x \setminus \{x\}} \sum_{|Y| = k} [Y \subseteq F_x(X)] \\
& = 2 \sum_{X \subseteq T_x \setminus \{x\}} \sum_{X_1 \subseteq T_{y_1}} \cdots \sum_{X_m \subseteq T_{y_m}} \left[ \bigcup_{i=0}^m X_i = X \right] \sum_{|Y| = k} \sum_{Y_1 \subseteq G_{y_1}(X)} \cdots \sum_{Y_m \subseteq G_{y_m}(X)} \left[\bigcup_{i=0}^m Y_i = Y\right] \\
& = 2 \sum_{X_1 \subseteq T_{y_1}} \cdots \sum_{X_m \subseteq T_{y_m}} \sum_{Y_1 \subseteq G_{y_1}(X)} \cdots \sum_{Y_m \subseteq G_{y_m}(X)} \left[\sum_{i=0}^m |Y_i| = k\right] \\
& = 2 \sum_{k_1+k_2+\dots+k_m = k} \left( \sum_{X_1 \subseteq T_{y_1}} \sum_{|Y_1| = k_1} [Y_1 \subseteq G_{y_1}(X)] \right) \cdots \left( \sum_{X_m \subseteq T_{y_m}} \sum_{|Y_m| = k_m} [Y_m \subseteq G_{y_m}(X)] \right) \\
& = 2 \sum_{k_1+k_2+\dots+k_m = k} g[y_1][k_1] \dots g[y_m][k_m]. \\
\end{aligned}
$$
我们可以看到$f[x][k]$的递推式是一个卷积的形式,可以在$O((\min\{\text{size}(x), k\})^2)$的时间复杂度内求解。
总时间复杂度为
$$ \sum_{x \in V} O((\min\{\text{size}(x), k\})^2) = O(kn). $$
CodeForces 1097G. Vladislav and a Great Legend的更多相关文章
- Codeforces 1097G Vladislav and a Great Legend [树形DP,斯特林数]
洛谷 Codeforces 这题真是妙的很. 通过看题解,终于知道了\(\sum_n f(n)^k\)这种东西怎么算. update:经过思考,我对这题有了更深的理解,现将更新内容放在原题解下方. ...
- Codeforces 1097G - Vladislav and a Great Legend(第二类斯特林数+树上背包)
Codeforces 题目传送门 & 洛谷题目传送门 首先看到这题我的第一反应是:这题跟这题长得好像,不管三七二十一先把 \(k\) 次方展开成斯特林数的形式,\(f(X)^k=\sum\li ...
- 1097G Vladislav and a Great Legend
传送门 分析 https://blog.csdn.net/forever_shi/article/details/88048528 代码 #include<iostream> #inclu ...
- Codeforces 1097 G. Vladislav and a Great Legend
题目链接 一道好题. 题意:给定一棵\(n\)个点的树,求: \[\sum_{S\subseteq \{1,2,\dots,n\}}f(S)^k\] 其中\(f(S)\)代表用树边将点集\(S\)连通 ...
- CF1097G Vladislav and a Great Legend
传送门 题目大意 一棵$n$个点的树,一个点集$S$的权值定义为把这个点击连成一个联通块的最少边数,求: $$ans=\sum_{S\in U}f(S)^k$$ 题解 这题跟gdoi那道题差不多 先把 ...
- Codeforces 1097G
根本想不到 CF1097G 题意 给出一棵树,定义f(S)为用最少的边连通点集$ S$的边数 求$ \sum\limits f(S)^k$ $ n \leq 10^5 k \leq 200$ 题解 假 ...
- CF1097G Vladislav and a Great Legend 组合、树形背包
传送门 看到\(k\)次幂求和先用斯特林数拆幂:\(x^k = \sum\limits_{i=1}^k \binom{x}{i}\left\{ \begin{array}{cccc} k \\ i \ ...
- codeforces 1136E-Nastya Hasn't Written a Legend
传送门:QAQQAQ 题意:有一个数组a和一个数组k,数组a一直保持一个性质:a[i + 1] >= a[i] + k[i].有两种操作:1,给某个元素加上x,但是加上之后要保持数组a的性质.比 ...
- 学习总结:斯特林数( Stirling number )
基本定义 第一类斯特林数:$1 \dots n$的排列中恰好有$k$个环的个数:或是,$n$元置换可分解为$k$个独立的轮换的个数.记作 $$ \begin{bmatrix} n \\ k \end{ ...
随机推荐
- Spring Cloud 微服务一:Consul注册中心
Consul介绍 Consul is a service mesh solution providing a full featured control plane with service disc ...
- 修改zend studio字符集
zend studio是一款编辑PHP的很好的工具,但是它的默认字符集是GBK,如何修改成UTF-8呢? 一.修改整个编辑器的编码 其实很简单,如果你做的每一个项目都是固定的某一个字符集(如UTF-8 ...
- 【BZOJ3796】Mushroom追妹纸 二分+hash
[BZOJ3796]Mushroom追妹纸 Description Mushroom最近看上了一个漂亮妹纸.他选择一种非常经典的手段来表达自己的心意——写情书.考虑到自己的表达能力,Mushroom决 ...
- The goroutine scheduler is not preemptive.
go - Why is time.sleep required to run certain goroutines? - Stack Overflow https://stackoverflow.co ...
- packages/wepy-web/src/wx.js 分析storage 的加载原理 wx.getStorage(OBJECT)
是小程序实例化后 读入内存 还是每次调用从文件系统读取 https://github.com/Tencent/wepy/blob/bd0003dca2bfb9581134e1b05d4aa1d80fc ...
- 阿里云ecs docker使用(4)---mongo docker
1.新建一个Dockerfile文件 vim Dockerfile #VERSION 0.1.0 FROM ubuntu:14.04 #Install some RUN apt-get clean ...
- 转换(旋转)transform
div { transform:rotate(180deg); -ms-transform:rotate(180deg); /* IE 9 */ -moz-transform:rotate(180de ...
- before-request , after-request
1 . flask的中间件 1)@app.before_request # 请求进入视图函数之前,类似于django中间件的request_process 2)@app.after_reque ...
- 【Leetcode-easy】Remove Duplicates from Sorted Array
题目要求:删除排好序的含有重复元素的数组.返回去除重复后的数组长度,并更新原始数组.不能使用额外空间. 思路:要不额外的使用内存空间,那么只有遍历数组,count保持下一个不重复的数字,遍历过程中如果 ...
- BeginPaint和GetDC有什么区别
windows编程问题 第一种情况显示出来的字很正常. case WM_PAINT: gdc = BeginPaint (hwnd, &ps); TextOut (gdc, 0, 0, s, ...