You are given a rooted tree with n nodes. The nodes are numbered 1..n. The root is node 1, and m of the nodes are colored red, the rest are black.

You would like to choose a subset of nodes such that there is no node in your subset which is an ancestor of any other node in your subset. For example, if A is the parent of B and B is the parent of C, then you could have at most one of A, B or C in your subset. In addition, you would like exactly k of your chosen nodes to be red.

If exactly mm of the nodes are red, then for all k = 0..m, figure out how many ways you can choose subsets with k red nodes, and no node is an ancestor of any other node.

Input Format

Each input will consist of a single test case.

Note that your program may be run multiple times on different inputs.

Each test case will begin with a line with two integers n(1≤n≤2×10^5) and m(0≤m≤min(10^3,n)), where n is the number of nodes in the tree, and m is the number of nodes which are red. The nodes are numbered 1..n.

Each of the next n - 1 lines will contain a single integer p(1≤p≤n), which is the number of the parent of this node. The nodes are listed in order, starting with node 2, then node 3, and so on. Node 1 is skipped, since it is the root. It is guaranteed that the nodes form a single tree, with a single root at node 1 and no cycles.

Each of the next m lines will contain single integer r(1≤r≤n). These are the numbers of the red nodes. No value of r will be repeated.

Output Format

Output m + 1 lines, corresponding to the number of subsets satisfying the given criteria with a number of red nodes equal to k = 0..m, in that order. Output this number modulo 10^9 + 7.

样例输入1

4 1
1
1
1
3

样例输出1

5
4

样例输入2

4 4
1
1
1
1
2
3
4

样例输出2

1
4
3
1
0

样例输入3

14 4
1
2
1
2
3
4
5
5
13
8
10
4
4
8
3
12
13

样例输出3

100
169
90
16
0

题意

一棵树,n个点,其中有m个红色,其余为黑点,1为根,选1个点集合,集合内的任意两点不互为祖先,问集合内红色节点的个数为0-m的方案数。

题解

dp[root][m]代表根为root的子树红色节点为m的方案数。

不选root,dp[root][0]=1,考虑子树son的情况,考虑h[M]代表组成M的方案数,相当于每次一颗子树把里面的所有红色节点一个一个压进去,类似于背包。

选root,dp[root][red[root]]++,相当于下面都不能选。

代码

 #include<bits/stdc++.h>
using namespace std;
const int N=;
const int M=;
const int MD=;
int dp[N][M],h[M],red[N],sz[N];
int n,m;
vector<int>G[N];
void dfs(int u){
dp[u][]=;
sz[u]=red[u];
for(int i=;i<G[u].size();i++) {
int v=G[u][i];
dfs(v);
int up=min(sz[u]+sz[v],m);
for(int j=;j<=up;j++)h[j]=;
for(int j=;j<=sz[v];j++)
for(int k=;k<=sz[u]&&j+k<=up;k++)
h[j+k]=(h[j+k]+1LL*dp[v][j]*dp[u][k])%MD;
sz[u]+=sz[v];
for(int j=;j<=sz[u];j++)dp[u][j]=h[j];
}
dp[u][red[u]]=(dp[u][red[u]]+)%MD;
}
int main() {
scanf("%d%d",&n,&m);
for(int i=,u;i<=n;i++) {
scanf("%d",&u);
G[u].push_back(i);
}
for(int i=,x;i<m;i++) {
scanf("%d",&x);
red[x]=;
}
dfs();
for(int i=;i<=m;i++)
printf("%d\n",dp[][i]);
return ;
}

计蒜客 Red Black Tree(树形DP)的更多相关文章

  1. 计蒜客 取数游戏 博弈+dp

    题目链接 取数游戏 思路:dp(x, y)表示先手在区间[x, y]能取得的最大分数.当先手取完,就轮到后手去,后手一定会选择当前能令他得到最大分数的策略,其实当先手在[x, y]区间两端取走一个数, ...

  2. 计蒜客 蓝桥杯模拟 瞬间移动 dp

      在一个 n \times mn×m 中的方格中,每个格子上都有一个分数,现在蒜头君从 (1,1)(1,1) 的格子开始往 (n, m)(n,m) 的格子走.要求从 (x_1,y_1)(x1​,y1 ...

  3. 计蒜客 取数游戏(dp)

    有如下一个双人游戏:N个正整数的序列放在一个游戏平台上,两人轮流从序列的两端取数,每次有数字被一个玩家取走后,这个数字被从序列中去掉并累加到取走该数的玩家的得分中,当数取尽时,游戏结束.以最终得分多者 ...

  4. 计蒜客 Flashing Fluorescents(状压DP)

    You have nn lights, each with its own button, in a line. Pressing a light’s button will toggle that ...

  5. 计蒜客 31436 - 提高水平 - [状压DP]

    题目链接:https://nanti.jisuanke.com/t/31436 作为一名车手,为了提高自身的姿势水平,平时的练习是必不可少的.小 J 每天的训练包含 $N$ 个训练项目,他会按照某个顺 ...

  6. 计蒜客 31434 - 广场车神 - [DP+前缀和]

    题目链接:https://nanti.jisuanke.com/t/31434 小 D 是一位著名的车手,他热衷于在广场上飙车.每年儿童节过后,小 D 都会在广场上举行一场别样的车技大赛. 小 D 所 ...

  7. [计蒜客] 矿石采集【记搜、Tarjan缩点+期望Dp】

    Online Judge:计蒜客信息学3月提高组模拟赛 Label:记搜,TarJan缩点,树状数组,期望Dp 题解 整个题目由毫无关联的两个问题组合成: part1 问题:对于每个询问的起点终点,求 ...

  8. 计蒜客 NOIP 提高组模拟竞赛第一试 补记

    计蒜客 NOIP 提高组模拟竞赛第一试 补记 A. 广场车神 题目大意: 一个\(n\times m(n,m\le2000)\)的网格,初始时位于左下角的\((1,1)\)处,终点在右上角的\((n, ...

  9. 2019计蒜客信息学提高组赛前膜你赛 #2(TooYoung,TooSimple,Sometimes Naive

    计蒜客\(2019CSP\)比赛第二场 巧妙爆零这场比赛(我连背包都不会了\(QWQ\) \(T1\) \(Too\) \(Young\) 大学选课真的是一件很苦恼的事呢! \(Marco\):&qu ...

随机推荐

  1. canvas石英钟

    canvas石英钟:demo <!DOCTYPE html> <html> <head lang="en"> <meta charset= ...

  2. PHP函数高级(二)

    PHP函数基础:https://www.cnblogs.com/lxwphp/p/9867840.html   1.函数分类: 定义:完成某些功能的代码段 系统函数:字符串,数组,数字,日期时间 自定 ...

  3. java中 &&与& ||与| 的区别

    public class Demo { public static void main(String[] args) { int i = 5; int j = 3; // || 与 | 的区别 boo ...

  4. NYOJ--860 又见01背包(01背包)

    题目http://acm.nyist.net/JudgeOnline/problem.php?pid=860 分析:题目和普通的01背包问题一样,但是唯一不同的是数据的特殊性. 如果10^9根本就开辟 ...

  5. python jieba模块详解

    借鉴于 [jieba 模块文档] 用于自己学习和记录! jieba 模块是一个用于中文分词的模块 此模块支持三种分词模式 精确模式(试图将句子最精确的切开,适合文本分析) 全模式(把句子在所有可以成词 ...

  6. [NOI2015] 软件包管理器【树链剖分+线段树区间覆盖】

    Online Judge:Luogu-P2146 Label:树链剖分,线段树区间覆盖 题目大意 \(n\)个软件包(编号0~n-1),他们之间的依赖关系用一棵含\(n-1\)条边的树来描述.一共两种 ...

  7. Leetcode959. Regions Cut By Slashes由斜杠划分区域

    在由 1 x 1 方格组成的 N x N 网格 grid 中,每个 1 x 1 方块由 /.\ 或空格构成.这些字符会将方块划分为一些共边的区域. (请注意,反斜杠字符是转义的,因此 \ 用 &quo ...

  8. Ubuntu时间管理方法

    1. date 命令主要用于显示以及修改系统时间 2. hwclock 命令用于查看设置硬件时间,以及同步硬件时间与系统时间 # 显示硬件时间hwclock # 设置硬件时间hwclock -set ...

  9. IIS+PHP+MYSQL搭建

    以下安装过程是在win7环境下: mysql安装参照前面windows下的mysql zip格式安装.下面主要讲除mysql以外的安装. 一.IIS安装 确保CGI被安装. 二.IIs安装成功后,安装 ...

  10. centos7 盘符变动 绑定槽位

    服务器下的硬盘主有机械硬盘.固态硬盘以及raid阵列,通常内核分配盘符的顺序是/dev/sda./dev/sdb… ….在系统启动过程中,内核会按照扫描到硬盘的顺序分配盘符(先分配直通的,再分配阵列) ...