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\)是我很不 ...
随机推荐
- webpack之proxyTable设置跨域
可以看看这个视频中的介绍 :https://ke.qq.com/course/350693?tuin=f158ed6
- git的操作集合
一.git 操作 $ git branch -a //查看远程分支$ git branch //查看本地分支 $ git branch -d <BranchName>//删除本地分支$ g ...
- Spring Boot系列(二) Spring Boot 之 REST
Rest (Representational Stat Transer) 是一种软件架构风格. 基础理论 架构特性 性能 可伸缩 简化的统一接口 按需修改 组件通信透明 可移植 可靠性 架构约束 C/ ...
- nodejs的package.json依赖dependencies中 ^ 和 ~ 的区别
nodejs的package.json定义了一个模块,包括其依赖关系的一个简单的JSON文件,该文件可以包含多个不同的指令来告诉Node包管理器如何处理模块. dependencies则表示此模块依赖 ...
- CocoaLumberjack——带颜色的Log
CocoaLumberjack可以带颜色Log,具体的好处嘛,谁用谁知道,:] 具体步骤如下: 1. 安装XcodeColors插件 下载地址:https://github.com/robbiehan ...
- WPF 10天修炼 第二天- XAML语言
XAML是什么 XAML是一种与.NET CLR紧密集成的声明性UI标记语言.XAML中的对象元素对应到CLR中的类型或结构.XAML命名空间对应到CLR中类的命名空间,元素类型则对应到CLR中的类型 ...
- C# 登陆验证码工具类VerifyCode
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; ...
- 【转】iPython入门技巧
[转]http://www.cnblogs.com/cuiyubo/p/6823478.html 学习<利用python进行数据分析> 第三章 IPython:一种交互式计算和开发环境的笔 ...
- SpringMVC-Helloworld 的归纳理解
前面使用SpringMVC写了Helloworld, 发现理解不是很深刻,很多东西只是跟着教学视频敲才会 现在那Helloworld以及一般的SpringMVC归纳一下: SpringMVC入门Hel ...
- org/eclipse/jetty/server/Handler : Unsupported major.minor version 52.0
注:本文来源于<org/eclipse/jetty/server/Handler : Unsupported major.minor version 52.0> Exception in ...