Codeforces 461B 树DP 解题报告

B. Appleman and Tree

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).

Input

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 ertices 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 to 1, vertex i is colored black. Otherwise, vertex i is colored white.

Output

Output a single integer — the number of ways to split the tree modulo 1000000007 (109 + 7).

Examples

input
3
0 0
0 1 1
output
2
input
6
0 1 1 0 4
1 1 0 0 1 0
output
1
input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
output
27

【解题报告】
题意:给出一棵树,每个点是白色或者黑色,问有多少种方案能够通过去掉一些边使每个联通块中只有一个黑色的点。

定义状态dp[i][0…1]表示点i在以点i为根的子树中去掉边后点i所在的联通块有黑点(1)和没有黑点(0)的方案数。
1.那么我们对于点u,首先考虑它的颜色,如果是黑色,那么dp[i][1] = 1 , 否则dp[i][0] = 1。
——1.1然后我们考虑给点u这个根添加一个子树,如果是添加一棵树根所在联通块有黑点的子树,也就是dp[v][1]
————1.1.1如果当前点u已经在有黑点的联通块中,那么就只有一种情况,就是当前边选择剪掉,那么方案数就是dp[u][1]⋅dp[v][1]
————1.1.2如果当前点u在一个没有黑点的联通块当中,那么可以有两种选择,一种是剪掉一种是保留,那么保留的方案数就是dp[u][0]⋅dp[v][1],减掉的方案数就是dp[u][0]*dp[v][1]
——1.2如果添加一棵根所在连通块没有黑点的子树,也就是dp[v][0]
————1.2.1如果当前点u已经在有黑点的连通块中,那么就只有一个情况,那么当前边只能保留,因为不能存在一个没有黑点的连通块,那么选择保留的方案数就是dp[u][1]⋅dp[v][0]
————1.2.1如果当前点u没有在有黑点的连通块中,就只有一个情况,那么就是保留,因为当前点只有在与u相连的时候才有可能找到一个黑点放入它的连通块,那么选择保留的方案数就是dp[u][0]∗dp[v][0]。

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 100010
#define mod 1000000007
#define LL long long

int n,w[N];
int cnt=-1,head[N];
struct Edge{int to,nxt;}e[N<<1];
LL dp[N][2];

void adde(int u,int v)
{
    e[++cnt].to=v;e[cnt].nxt=head[u];head[u]=cnt;
    e[++cnt].to=u;e[cnt].nxt=head[v];head[v]=cnt;
}
void dfs(int u,int fa)
{
    if(w[u]) dp[u][1]=1;
    else dp[u][0]=1;
    for(int i=head[u];~i;i=e[i].nxt)
    {
        int v=e[i].to;
        if(v==fa) continue;
        dfs(v,u);
        dp[u][1]=(dp[u][1]*(dp[v][0]+dp[v][1])+dp[u][0]*dp[v][1])%mod;
        dp[u][0]=dp[u][0]*(dp[v][0]+dp[v][1])%mod;
    }
}
int main()
{
    memset(head,-1,sizeof(head));
    scanf("%d",&n);
    for(int i=1;i<n;++i)
    {
        int tmp;scanf("%d",&tmp);
        adde(i,tmp);
    }
    for(int i=0;i<n;++i) scanf("%d",&w[i]);
    dfs(0,0);
    printf("%lld\n",dp[0][1]);
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值