传送门


题目大意

一棵$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

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <algorithm>
  5. #define LL long long
  6. using namespace std;
  7. const LL Maxn = 100010;
  8. const LL Maxk = 210;
  9. const LL Mod = 1e9+7;
  10. LL f[Maxn][Maxk], g[Maxk];
  11. LL h[Maxk];
  12. LL n, K;
  13. struct node {
  14. LL y, next;
  15. }a[Maxn<<1]; LL first[Maxn], len;
  16. void ins(LL x, LL y) {
  17. len++;
  18. a[len].y = y;
  19. a[len].next = first[x]; first[x] = len;
  20. }
  21. LL Stir2[Maxk][Maxk], jc[Maxk];
  22. LL siz[Maxn];
  23. void up(LL &x, LL y) { x = (x + y) % Mod; }
  24. void dfs(LL x, LL fa) {
  25. f[x][0] = 2;
  26. siz[x] = 1;
  27. for(LL k = first[x]; k; k = a[k].next){
  28. LL y = a[k].y;
  29. if(y == fa) continue;
  30. dfs(y, x);
  31. for(LL i = 0; i < siz[x]+siz[y] && i <= K; i++) g[i] = 0;
  32. for(LL i = 0; i < siz[x] && i <= K; i++){
  33. for(LL j = 0; j <= siz[y] && j <= K-i; j++) up(g[i+j], f[x][i]*f[y][j]);
  34. }
  35. siz[x] += siz[y];
  36. for(LL i = 0; i < siz[x] && i <= K; i++) f[x][i] = g[i];
  37. }
  38. if(x != 1){
  39. for(LL i = 0; i < K; i++){
  40. up(h[i+1], Mod-f[x][i]);
  41. if(i == 0) up(h[1], 1);
  42. }
  43. } else for(LL i = 1; i <= K; i++) up(h[i], f[x][i]);
  44. for(LL i = K; i > 0; i--) up(f[x][i], f[x][i-1]);
  45. up(f[x][1], Mod-1);
  46. }
  47. int main() {
  48. LL i, j, k;
  49. scanf("%lld%lld", &n, &K);
  50. Stir2[0][0] = 1;
  51. for(i = 1; i <= K; i++){
  52. for(j = 1; j <= i; j++) Stir2[i][j] = (j*Stir2[i-1][j]+Stir2[i-1][j-1])%Mod;
  53. }
  54. jc[0] = 1;
  55. for(i = 1; i <= K; i++) jc[i] = jc[i-1]*i%Mod;
  56. for(i = 1; i < n; i++){
  57. LL x, y;
  58. scanf("%lld%lld", &x, &y);
  59. ins(x, y); ins(y, x);
  60. }
  61. dfs(1, 0);
  62. LL ans = 0;
  63. for(i = 1; i <= K; i++) up(ans, Stir2[K][i]*jc[i]%Mod*h[i]);
  64. printf("%lld\n", ans);
  65. return 0;
  66. }

CF1097G Vladislav and a Great Legend的更多相关文章

  1. CF1097G Vladislav and a Great Legend 组合、树形背包

    传送门 看到\(k\)次幂求和先用斯特林数拆幂:\(x^k = \sum\limits_{i=1}^k \binom{x}{i}\left\{ \begin{array}{cccc} k \\ i \ ...

  2. Codeforces 1097G Vladislav and a Great Legend [树形DP,斯特林数]

    洛谷 Codeforces 这题真是妙的很. 通过看题解,终于知道了\(\sum_n f(n)^k​\)这种东西怎么算. update:经过思考,我对这题有了更深的理解,现将更新内容放在原题解下方. ...

  3. Codeforces 1097 G. Vladislav and a Great Legend

    题目链接 一道好题. 题意:给定一棵\(n\)个点的树,求: \[\sum_{S\subseteq \{1,2,\dots,n\}}f(S)^k\] 其中\(f(S)\)代表用树边将点集\(S\)连通 ...

  4. 1097G Vladislav and a Great Legend

    传送门 分析 https://blog.csdn.net/forever_shi/article/details/88048528 代码 #include<iostream> #inclu ...

  5. CodeForces 1097G. Vladislav and a Great Legend

    题目简述:给定$n \leq 10^5$个节点的树$T = (V, E)$,令$X \subseteq V$表示一个非空节点集合,定义$f(X)$为包含$X$的最小子树的边数.求 $$ \sum_{\ ...

  6. Codeforces 1097G - Vladislav and a Great Legend(第二类斯特林数+树上背包)

    Codeforces 题目传送门 & 洛谷题目传送门 首先看到这题我的第一反应是:这题跟这题长得好像,不管三七二十一先把 \(k\) 次方展开成斯特林数的形式,\(f(X)^k=\sum\li ...

  7. Hello 2019 (D~G)

    目录 Codeforces 1097 D.Makoto and a Blackboard(DP 期望) E.Egor and an RPG game(思路 LIS Dilworth定理) F.Alex ...

  8. 学习总结:斯特林数( Stirling number )

    基本定义 第一类斯特林数:$1 \dots n$的排列中恰好有$k$个环的个数:或是,$n$元置换可分解为$k$个独立的轮换的个数.记作 $$ \begin{bmatrix} n \\ k \end{ ...

  9. 『正睿OI 2019SC Day6』

    动态规划 \(dp\)早就已经是经常用到的算法了,于是老师上课主要都在讲题.今天讲的主要是三类\(dp\):树形\(dp\),计数\(dp\),\(dp\)套\(dp\).其中计数\(dp\)是我很不 ...

随机推荐

  1. linux常用系统指令

    [linux常用系统指令] 查看内核版本:cat /proc/version 查看发行版本:cat /etc/issue 通过安装lsb的方式查看发行版本: yum provides */lsb_re ...

  2. Studio 5000编程:一种累计时间的编程方法

    前言:在很多项目中,需要累计设备的运行.停机.故障时间,当然实现该功能的编程方法也是多种多样,各有千秋,不过有的方法累计误差会越来越大,比如:在连续任务里用定时器来累计时间,就存在一定的误差.本文分享 ...

  3. Linux命令--tree

    目录 tree 最常用 带颜色显示2级目录 排除显示某个目录 tree tree -C :颜色显示 tree -f : 显示文件全路径 tree -L 2 :只显示2层 tree -P *.pl :只 ...

  4. JavaScript表单验证的相关事件

    1.  表单元素: a)         Input标签:文本框(text)—密码框(password)—单选—复选框—文件—图像—隐藏—按钮—提交—重置,表单元素都在input标签 b)       ...

  5. STL--sort源码分析

    SGI STL sort源码 temlate <class RandowAccessIterator> inline void sort(RandowAccessIterator firs ...

  6. [转] 图解Seq2Seq模型、RNN结构、Encoder-Decoder模型 到 Attention

    from : https://caicai.science/2018/10/06/attention%E6%80%BB%E8%A7%88/ 一.Seq2Seq 模型 1. 简介 Sequence-to ...

  7. Power BI行级别安全性(数据权限管理)

    自从PowerBI 的DAX 函数 支持username() 或 userprincipalname()的函数后,我们就可以在Power BI中实现根据用户的行级数据权限的控制. username() ...

  8. Mybatis 笔记

    环境:Mybatis 3 +MariaDB 10.1 似乎在调用存储过程时 ,参数只能写在一行上. 否则会返回语法错误.

  9. linux部署dns内网服务器

    安装: yum -y install bind* 编辑named.conf vim /etc/named.conf options { listen-on port { any; }; listen- ...

  10. bzoj 2599

    还是点对之间的问题,果断上点分治 同样,把一条路径拆分成经过根节点的两条路径,对不经过根节点的路径递归处理 然后,我们逐个枚举根节点的子树,计算出子树中某一点到根节点的距离,然后在之前已经处理过的点中 ...