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{ ...
随机推荐
- 漫反射和Lambert模型
粗糙的物体表面向各个方向等强度地反射光,这种等同地向各个方向散射的现象称为光的漫反射(diffuse reflection).产生光的漫反射现象的物体表面称为理想漫反射体,也称为朗伯(Lambert) ...
- python 基础 8.3 match方法和search方法
一,正则对象的split 方法 split(string[,maxsplit]) 按照能够匹配的字串讲string 分割后返回列表.maxsplit 用于指定最大分割次数,不指定将全部分割.来查找符合 ...
- 4276: [ONTAK2015]Bajtman i Okrągły Robin
4276: [ONTAK2015]Bajtman i Okrągły Robin Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 345 Solved ...
- 【题解】Codeforces 961G Partitions
[题解]Codeforces 961G Partitions cf961G 好题啊哭了,但是如果没有不小心看了一下pdf后面一页的提示根本想不到 题意 已知\(U=\{w_i\}\),求: \[ \s ...
- ABap-小技巧
if FIELD cn '0123456789'. *&如果字符串包含‘数字’ STOP. endif. 同理到字母‘ABCDEFG*’ 'abcdefg*' '/' '\' 等其它字 ...
- redis安装包下载
redis linux版安装包下载地址 http://download.redis.io/releases/
- Elasticsearch5 及 head插件 安装说明
Elasticsearch5.X及 head插件 安装说明: 1.下载elasticsearch安装文件: a) 下载官方源码: https://artifacts.elastic.co/downlo ...
- Machine Learning No.2: Linear Regression with Multiple Variables
1. notation: n = number of features x(i) = input (features) of ith training example = value of feat ...
- Android Weekly Notes Issue #321
Android Weekly Issue #321 August 5th, 2018. Android Weekly Issue #321 本期内容包括: 开源项目Plaid的改版; 使用Tensor ...
- Sprin Boot2.0之整合Mybatis整合分页插件
pageHelper PageHelper 是一款好用的开源免费的 Mybatis 第三方物理分页插件 物理分页 支持常见的 12 种数据库.Oracle,MySql,MariaDB,SQLite,D ...