CodeCraft-19 and Codeforces Round #537 (Div. 2) E 虚树 + 树形dp(新坑)
https://codeforces.com/contest/1111/problem/E
题意
一颗有n个点的树,有q个询问,每次从树挑出k个点,问将这k个点分成m组,需要保证在同一组中不存在一个点是另一个点的祖先这种情况,问有多少中分组方案
题解
- 首先解决转移问题,假设一次询问全颗树的分组方案数,定义dp[u][i]为到树节点u,分了i组的方案数,其中num(fa[u])为u的祖先节点个数
- \(dp[u][i]=dp[fa][i]*(i-num(fa[u]))+dp[fa][i-1]\)
- 当前这个点独立分组:\(dp[fa][i-1]->dp[u][i]\)
- 当前这个点加入前面的分组:\(dp[fa][i]*(i-num(fa[u]))->dp[u][i]\)
- 反着扫,滚掉一维
- 然后每次询问一些点,但点询问的总规模数和总点数相同的这种问题可以考虑用虚树解决
代码
#include<bits/stdc++.h>
#define MOD 1000000007
#define MAXN 100005
#define MAXM 305
#define MAXK 100005
#define ll long long
using namespace std;
int n,i,q,u,v,sp[MAXN],dfn[MAXN],dep[MAXN],f[MAXN][40],rt;
ll ans,dp[MAXM];
int k,m,r,ti,S[MAXK];
vector<int>g[MAXN];
vector<int>vt[MAXK];
bool cmp(int x,int y){
return dfn[x]<dfn[y];
}
void dfs1(int u,int fa){
dfn[u]=++ti;
dep[u]=dep[fa]+1;
f[u][0]=fa;
for(int i=1;i<=20;i++)f[u][i]=f[f[u][i-1]][i-1];
for(auto v:g[u])
if(v!=fa)dfs1(v,u);
}
int lca(int u,int v){
if(dep[u]<dep[v])swap(u,v);
int dis=dep[u]-dep[v];
for(int i=20;i>=0;i--)if((dis>>i)&1)u=f[u][i];
if(u==v)return u;
for(int i=20;i>=0;i--){
if(f[u][i]!=f[v][i]){
u=f[u][i];
v=f[v][i];
}
}
return f[u][0];
}
void add_edge(int u,int v){
vt[u].push_back(v);
vt[v].push_back(u);
}
void build_vtree(vector<int>& a){
sort(a.begin(),a.end(),cmp);
a.erase(unique(a.begin(),a.end()),a.end());
int t=0;
S[t++]=a[0];
vector<int>b;b.clear();
for(int i=1;i<a.size();i++){
if(t==0){S[t++]=a[i];continue;}
int l=lca(a[i],S[t-1]);
while(t>1&&dfn[S[t-2]]>=dfn[l]){add_edge(S[t-2],S[t-1]);t--;}
if(l!=S[t-1]){add_edge(S[t-1],l);S[t-1]=l;b.push_back(l);}
S[t++]=a[i];
}
while(t>1){add_edge(S[t-1],S[t-2]);t--;}
for(auto u:b)a.push_back(u);
}
void dfs2(int u,int fa,int cnt){
if(sp[u]){
for(int i=m;i>=1;i--){
if(i>cnt)dp[i]=dp[i]*(i-cnt)%MOD;
else dp[i]=0;
dp[i]=(dp[i]+dp[i-1])%MOD;
}
dp[0]=0;
cnt++;
}
for(int i=0;i<vt[u].size();i++){
int v=vt[u][i];if(v==fa)continue;
dfs2(v,u,cnt);
}
}
int main(){
cin>>n>>q;
for(int i=0;i<n-1;i++){
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
dfs1(1,0);
while(q--){
scanf("%d%d%d",&k,&m,&r);
vector<int>nodes;nodes.clear();
for(int i=0;i<k;i++){
scanf("%d",&u);
nodes.push_back(u);
sp[u]=1;
}
nodes.push_back(r);
build_vtree(nodes);
memset(dp,0,sizeof(dp));
dp[0]=1;
dfs2(r,0,0);
ans=0;
for(int i=0;i<=m;i++)ans=(ans+dp[i])%MOD;
printf("%lld\n",ans);
for(auto u:nodes)sp[u]=0;
for(auto u:nodes)vt[u].clear();
}
}
CodeCraft-19 and Codeforces Round #537 (Div. 2) E 虚树 + 树形dp(新坑)的更多相关文章
- Codeforces Round #530 (Div. 2) F 线段树 + 树形dp(自下往上)
https://codeforces.com/contest/1099/problem/F 题意 一颗n个节点的树上,每个点都有\(x[i]\)个饼干,然后在i节点上吃一个饼干的时间是\(t[i]\) ...
- Codeforces Round #536 (Div. 2) F 矩阵快速幂 + bsgs(新坑) + exgcd(新坑) + 欧拉降幂
https://codeforces.com/contest/1106/problem/F 题意 数列公式为\(f_i=(f^{b_1}_{i-1}*f^{b_2}_{i-2}*...*f^{b_k} ...
- Codeforces Round #549 (Div. 2) F 数形结合 + 凸包(新坑)
https://codeforces.com/contest/1143/problem/F 题意 有n条形如\(y=x^2+bx+c\)的抛物线,问有多少条抛物线上方没有其他抛物线的交点 题解 \(y ...
- Codeforces Round #530 (Div. 2)F Cookies (树形dp+线段树)
题:https://codeforces.com/contest/1099/problem/F 题意:给定一个树,每个节点有俩个信息x和t,分别表示这个节点上的饼干个数和先手吃掉这个节点上一个饼干的的 ...
- Codeforces Round #426 (Div. 2) D 线段树优化dp
D. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...
- Codeforces Round #302 (Div. 1) D - Road Improvement 树形dp
D - Road Improvemen 思路:0没有逆元!!!! 不能直接除,要求前缀积和后缀积!!! #include<bits/stdc++.h> #define LL long lo ...
- Codeforces Round #367 (Div. 2) C. Hard problem(DP)
Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...
- CodeCraft-19 and Codeforces Round #537 Div. 2
D:即有不超过52种物品,求容量为n/2的有序01背包方案数.容易想到设f[i][j]为前i种物品已用容量为j的方案数,有f[i][j]=f[i-1][j-a[i]]*C(n/2-j+a[i],a[i ...
- Codeforces Beta Round #14 (Div. 2) D. Two Paths 树形dp
D. Two Paths 题目连接: http://codeforces.com/contest/14/problem/D Description As you know, Bob's brother ...
随机推荐
- background-size的应用情景:当给出的背景图片大于实际网页需要布局的图片大小时
网页需求是:50*50 如果只设置 width:50px;height:50px;background-image("images/XXX.png"); 效果如下: 添加设置:b ...
- gentoo 图像方面的软件
图像方面的软件一般包括:查看图像,屏幕截图,图像修改. 查看图像简单的可以安装 feh,但是 feh 一般作为墙纸来用.稍微好一些的是 gqview. 屏幕截图可以用 screengrab,使用的时候 ...
- C#操作IIS程序池及站点的创建配置(转)
原文:http://www.cnblogs.com/wujy/archive/2013/02/28/2937667.html 最近在做一个WEB程序的安装包:对一些操作IIS进行一个简单的总结:主 ...
- SQL 读取csv 文件批量插入数据
use test /* create table temp_pre ( vc_product_id varchar(20) default '', en_in_amount numeric(9,2)d ...
- SQL Server error
原因:文件没有权限 出错: TITLE: Microsoft SQL Server Management Studio------------------------------ Attach dat ...
- 分布式版本控制系统Git的安装和使用
作业要求来自https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2097 GitHub远程仓库的地址:https://github.com/ ...
- 看到blackarch 字体不错 记录下来
terminus-font
- nginx实现http www服务的方式
- EasyUI datagrid单元格文本超出显示省略号,鼠标移动到单元格显示文本
nowrap : true; 是前提 $('#×××').datagrid({ nowrap : true,//设置为true,当数据长度超出列宽时将会自动截取 }); 省略号样式: <sty ...
- Mobile Computing: the Next Decade论文 cloudlet薄云
1 Introduction “Information at your fingertips anywhere, anytime” has been the driving vision of mob ...