CF1097G Vladislav and a Great Legend
题目大意
一棵$n$个点的树,一个点集$S$的权值定义为把这个点击连成一个联通块的最少边数,求:
$$ans=\sum_{S\in U}f(S)^k$$
题解
这题跟gdoi那道题差不多
先把柿子化一下变成
$$ans=\sum_{i=0}^k \begin{Bmatrix}k\\i\end{Bmatrix} i! \sum_{S\in U}\begin{pmatrix}f(S)\\i\end{pmatrix}$$
然后我们就相当于去统计大小为$i$的边集的贡献
这个可以通过dp来实现
定义$f_{x,i}$表示$x$子树内所有点与父亲的连边中选出了$i$条边,子树内选择的点的方案数
dp过程就是首先算出不包括$x$和父亲的边的方案数,然后再加上这条边就可以了
在$x$和父亲的边加入边集时,要注意$x$的子树内外会不会一个点都没选
减去这些方案就可以了
Code
- #include <cstdio>
- #include <cstring>
- #include <cstdlib>
- #include <algorithm>
- #define LL long long
- using namespace std;
- const LL Maxn = 100010;
- const LL Maxk = 210;
- const LL Mod = 1e9+7;
- LL f[Maxn][Maxk], g[Maxk];
- LL h[Maxk];
- LL n, K;
- struct node {
- LL y, next;
- }a[Maxn<<1]; LL first[Maxn], len;
- void ins(LL x, LL y) {
- len++;
- a[len].y = y;
- a[len].next = first[x]; first[x] = len;
- }
- LL Stir2[Maxk][Maxk], jc[Maxk];
- LL siz[Maxn];
- void up(LL &x, LL y) { x = (x + y) % Mod; }
- void dfs(LL x, LL fa) {
- f[x][0] = 2;
- siz[x] = 1;
- for(LL k = first[x]; k; k = a[k].next){
- LL y = a[k].y;
- if(y == fa) continue;
- dfs(y, x);
- for(LL i = 0; i < siz[x]+siz[y] && i <= K; i++) g[i] = 0;
- for(LL i = 0; i < siz[x] && i <= K; i++){
- for(LL j = 0; j <= siz[y] && j <= K-i; j++) up(g[i+j], f[x][i]*f[y][j]);
- }
- siz[x] += siz[y];
- for(LL i = 0; i < siz[x] && i <= K; i++) f[x][i] = g[i];
- }
- if(x != 1){
- for(LL i = 0; i < K; i++){
- up(h[i+1], Mod-f[x][i]);
- if(i == 0) up(h[1], 1);
- }
- } else for(LL i = 1; i <= K; i++) up(h[i], f[x][i]);
- for(LL i = K; i > 0; i--) up(f[x][i], f[x][i-1]);
- up(f[x][1], Mod-1);
- }
- int main() {
- LL i, j, k;
- scanf("%lld%lld", &n, &K);
- Stir2[0][0] = 1;
- for(i = 1; i <= K; i++){
- for(j = 1; j <= i; j++) Stir2[i][j] = (j*Stir2[i-1][j]+Stir2[i-1][j-1])%Mod;
- }
- jc[0] = 1;
- for(i = 1; i <= K; i++) jc[i] = jc[i-1]*i%Mod;
- for(i = 1; i < n; i++){
- LL x, y;
- scanf("%lld%lld", &x, &y);
- ins(x, y); ins(y, x);
- }
- dfs(1, 0);
- LL ans = 0;
- for(i = 1; i <= K; i++) up(ans, Stir2[K][i]*jc[i]%Mod*h[i]);
- printf("%lld\n", ans);
- return 0;
- }
CF1097G Vladislav and a Great Legend的更多相关文章
- CF1097G Vladislav and a Great Legend 组合、树形背包
传送门 看到\(k\)次幂求和先用斯特林数拆幂:\(x^k = \sum\limits_{i=1}^k \binom{x}{i}\left\{ \begin{array}{cccc} k \\ i \ ...
- Codeforces 1097G Vladislav and a Great Legend [树形DP,斯特林数]
洛谷 Codeforces 这题真是妙的很. 通过看题解,终于知道了\(\sum_n f(n)^k\)这种东西怎么算. update:经过思考,我对这题有了更深的理解,现将更新内容放在原题解下方. ...
- Codeforces 1097 G. Vladislav and a Great Legend
题目链接 一道好题. 题意:给定一棵\(n\)个点的树,求: \[\sum_{S\subseteq \{1,2,\dots,n\}}f(S)^k\] 其中\(f(S)\)代表用树边将点集\(S\)连通 ...
- 1097G Vladislav and a Great Legend
传送门 分析 https://blog.csdn.net/forever_shi/article/details/88048528 代码 #include<iostream> #inclu ...
- CodeForces 1097G. Vladislav and a Great Legend
题目简述:给定$n \leq 10^5$个节点的树$T = (V, E)$,令$X \subseteq V$表示一个非空节点集合,定义$f(X)$为包含$X$的最小子树的边数.求 $$ \sum_{\ ...
- Codeforces 1097G - Vladislav and a Great Legend(第二类斯特林数+树上背包)
Codeforces 题目传送门 & 洛谷题目传送门 首先看到这题我的第一反应是:这题跟这题长得好像,不管三七二十一先把 \(k\) 次方展开成斯特林数的形式,\(f(X)^k=\sum\li ...
- Hello 2019 (D~G)
目录 Codeforces 1097 D.Makoto and a Blackboard(DP 期望) E.Egor and an RPG game(思路 LIS Dilworth定理) F.Alex ...
- 学习总结:斯特林数( Stirling number )
基本定义 第一类斯特林数:$1 \dots n$的排列中恰好有$k$个环的个数:或是,$n$元置换可分解为$k$个独立的轮换的个数.记作 $$ \begin{bmatrix} n \\ k \end{ ...
- 『正睿OI 2019SC Day6』
动态规划 \(dp\)早就已经是经常用到的算法了,于是老师上课主要都在讲题.今天讲的主要是三类\(dp\):树形\(dp\),计数\(dp\),\(dp\)套\(dp\).其中计数\(dp\)是我很不 ...
随机推荐
- linux常用系统指令
[linux常用系统指令] 查看内核版本:cat /proc/version 查看发行版本:cat /etc/issue 通过安装lsb的方式查看发行版本: yum provides */lsb_re ...
- Studio 5000编程:一种累计时间的编程方法
前言:在很多项目中,需要累计设备的运行.停机.故障时间,当然实现该功能的编程方法也是多种多样,各有千秋,不过有的方法累计误差会越来越大,比如:在连续任务里用定时器来累计时间,就存在一定的误差.本文分享 ...
- Linux命令--tree
目录 tree 最常用 带颜色显示2级目录 排除显示某个目录 tree tree -C :颜色显示 tree -f : 显示文件全路径 tree -L 2 :只显示2层 tree -P *.pl :只 ...
- JavaScript表单验证的相关事件
1. 表单元素: a) Input标签:文本框(text)—密码框(password)—单选—复选框—文件—图像—隐藏—按钮—提交—重置,表单元素都在input标签 b) ...
- STL--sort源码分析
SGI STL sort源码 temlate <class RandowAccessIterator> inline void sort(RandowAccessIterator firs ...
- [转] 图解Seq2Seq模型、RNN结构、Encoder-Decoder模型 到 Attention
from : https://caicai.science/2018/10/06/attention%E6%80%BB%E8%A7%88/ 一.Seq2Seq 模型 1. 简介 Sequence-to ...
- Power BI行级别安全性(数据权限管理)
自从PowerBI 的DAX 函数 支持username() 或 userprincipalname()的函数后,我们就可以在Power BI中实现根据用户的行级数据权限的控制. username() ...
- Mybatis 笔记
环境:Mybatis 3 +MariaDB 10.1 似乎在调用存储过程时 ,参数只能写在一行上. 否则会返回语法错误.
- linux部署dns内网服务器
安装: yum -y install bind* 编辑named.conf vim /etc/named.conf options { listen-on port { any; }; listen- ...
- bzoj 2599
还是点对之间的问题,果断上点分治 同样,把一条路径拆分成经过根节点的两条路径,对不经过根节点的路径递归处理 然后,我们逐个枚举根节点的子树,计算出子树中某一点到根节点的距离,然后在之前已经处理过的点中 ...