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\)是我很不 ...
随机推荐
- Timer定时方法(间隔时间后执行)
Timer time = new Timer(); time.schedule(new TimerTask() { @Override public void run() { // TODO Auto ...
- Leetcode#500. Keyboard Row(键盘行)
题目描述 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词.键盘如下图所示. 示例1: 输入: ["Hello", "Alaska", &quo ...
- ubuntu 安装 lamp
链接: http://www.cnblogs.com/CheeseZH/p/4694135.html
- module.ngdoc
译自Angular's module docs 1.模块 大部分的应用都有一个主要的方法来实例化,链接,引导.angular应用没有这个方法,而是用模块声明来替代. 这种方式的优点: *程序的声明越详 ...
- 堆,set,优先队列
当我们需要高效的完成以下操作时: 1.插入一个元素 2.取得最小(最大)的数值,并且删除 能够完成这种操作的数据结构叫做优先队列 而能够使用二叉树,完成这种操作的数据结构叫做堆(二叉堆) 堆与优先队列 ...
- Qt无法正确 sendMessage 的消息
项目背景: 项目需要将vc中的代码移植到Qt中,而且由于使用的SDK是32位,所以,Qt使用的版本是MinGW32,另外下载的也是官网最新的版本Qt5.11.1. 系统环境:Windows10 在将w ...
- SpringBoot 上传文件夹
前端代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- C# 常用类型校验Validate
using System.Text; using System.Text.RegularExpressions; namespace 落地页测试代码 { public class Validate { ...
- kmp算法 模板
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #inclu ...
- Calendar日历工具类
这个工具类有效的避免跨年的问题 先定义一个日期格式类型: SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:s ...