Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 ≤ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
The first line contains an integer n (2 ≤ n ≤ 105) — the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 ≤ pi ≤ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to1, vertex i is colored black. Otherwise, vertex i is colored white.
Output a single integer — the number of ways to split the tree modulo 1000000007 (109 + 7).
3
0 0
0 1 1
2
6
0 1 1 0 4
1 1 0 0 1 0
1
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
27
题目大意:
给出一棵树,每个节点都被标记了颜色(黑色or白色)。要求把这棵树的其中k条变切换,划分成k+1棵子树,每颗子树必须有1个黑色节点,求有多少种划分方法。
解法:
很典型的树形dp了。
第一步,明确问题的类型:
计数型DP,并非最大值或最小值DP,考虑到其结构是一个树形,很容易联想到树形DP。
第二步,划分阶段和状态:
阶段为v,即为每个节点;
状态为s,即为包括节点v以内的子树内有无黑节点,1表示有1个黑节点,0表示没有黑节点。
第三步,由于是计数型DP,就得明确先明确计数原理:
乘法原理与加法原理相结合。
乘法原理:用于v节点的所有child节点中,整个v节点有多少种方案,需要通过所有child节点,如果标上顺序(结果与访问child顺序无关,所以怎样的合法顺序都可以),可以认为是分为child_n个步骤,所以用乘法原理。
加法原理:用于v节点的单个child节点中,当v节点对单个child节点中,从v节点的角度来看,单个child节点的状态可以有多种状态,而不是多个步骤,所以用加 法原理。
第四步,确定状态转移方程:
由于v可能有两种状态,我们分两种状态进行讨论:
1. s = 0,即包括v的子树中没有黑节点,我们需要保证v和child所有节点均为白色。对于单个child节点中的多种状态,大致为v(白色)与child(白色)是在一个树内(不切边),v(白色)与child(黑色)是不在同一个树内(不切边):
dp[v][0] = dp[v][0]*dp[child][0] + dp[v][0]*dp[child][1]。 第一个即为不切,第二个为切。 可能有人会提出疑问,dp[v][0]*dp[child][0]也可以切啊,因为题目对此并没有进行限制,的确如此,但是我们只考虑计数,并不考虑每个子树的形态,所以在这里就显得多虑了。
2. s = 1,即包括v的子树中有一个黑节点,我们只需要保证v或child中有且只有1个为黑色就够了。对于单个child节点中的多种状态,大致分为 v(黑色)与child(白色)在一个树内,v(黑色)与child(黑色)不再一个树内,v(白色)与child(黑色)在一个树内。
dp[v][1] = dp[v][1]*dp[child][0] + dp[v][1]*dp[child][1] + dp[v][0]*dp[child][1].
PS:这里我们需要注意的是dp[v][0]*dp[child][1],这里的dp[v][0]必须是还没有对该child节点进行计数的,因为如若已经对child节点进行了计数,dp[v][0]就已经包括了该child节点,这里的dp[v][0]是表示之前步骤的dp[v][0],不应当包括该步骤。
第五步,初始值与边界条件:
从上述的状态转移方程中我们可以看出,dp[v][s]均受v节点的初始值的影响,因为是乘法原理。例如v如若是黑色,则dp[v][0]始终是0;如若v是白色,在初始时dp[v][0] = 1,统计了第一个child有黑色的情况,接下来也统计了其他child有黑色的情况,并没有遗漏。
所以dp[v][col[v]] = 1。
代码:
#include <cstdio>
#include <vector>
#include <iostream>
#define N_max 123456
#define modn 1000000007
#define LL long long
using namespace std;
int n, col[N_max];
LL dp[N_max][2];
vector <int> G[N_max];
void addedge(int x, int y) {
G[x].push_back(y);
}
void init() {
scanf("%d", &n);
for (int i = 0; i < n-1; i++) {
int tmp;
scanf("%d", &tmp);
addedge(tmp, i+1);
}
for (int i = 0; i < n; i++)
scanf("%d", &col[i]);
}
void dfs(int v) {
dp[v][col[v]] = 1;
for (int i = 0; i < G[v].size(); i++) {
int u = G[v][i];
dfs(u);
dp[v][1] = (dp[v][1]*dp[u][0] + dp[v][1]*dp[u][1] + dp[v][0]*dp[u][1]) % modn;
dp[v][0] = (dp[v][0]*dp[u][0] + dp[v][0]*dp[u][1]) % modn;
}
}
void solve() {
dfs(0);
cout << dp[0][1] << endl;
}
int main() {
init();
solve();
}