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\)是我很不 ...
随机推荐
- torchvision 作用
1. 提供主流的model,和常用数据集 2. 对 torch.utils.data.Dataset进行扩充,针对不同类别图像放入不同文件夹的数据进行读取, torchvision.datasets. ...
- Linux-Shell编程之判断文件类型
前言 如需使用本博文源码或者撰写文章,请注明博文来源:https://www.cnblogs.com/johnnyzen/p/10534386.html,劳动所得,侵权必究. 题目 設計一個shell ...
- mac air中编译安装swoole
本机php版本, 我的7.3.0 1 下载swoole源码 https://github.com/swoole/swoole-src/releases 我下载的版本是swoole-src-4.3.3. ...
- js中对cookie的操作及json数据与cookie结合的用法
cookie的使用 添加cookie 添加cookie:document.cookie = “key=value”; // 一次写入一个键值对 document.cookie = 'test1=hel ...
- 关于lnmp下 phalcon和tp框架下的nginx文件配置
vim /etc/nginx/sites-available/default 进入修改目录 1.正常项目配置 server { listen 80 default_server; listen [ ...
- C# - 设计模式 - 钩子模式
钩子模式 问题场景 如何控制抽象类的行为?解决办法是靠钩子!抽象类公布一个虚方法,由子类自行决定是否重写它,抽象类以钩子做判定,如果返回真则执行某个方法,否则不执行.为什么钩子不能是抽象的,因为如果钩 ...
- python&JSONP(Jquery篇)
采用Jquery发送跨域请求: <!DOCTYPE html> <html lang="en"> <head> <meta charset ...
- rsyncd启动脚本
#!/bin/bash ############################################################## # File Name: -.sh # Versi ...
- Numerical Analysis
PART1 <求解方程> 1,二分法 def bisect(f,a,b,TOL=0.000004): u_a = a u_b = b while(u_b-u_a)/2.0 > TO ...
- C++入门篇九
explicit关键字:防止构造函数隐式类型转换 #include <iostream> #include "pch.h" using namespace std; c ...