Codeforces 462D Appleman and Tree【思维+树型Dp】

351 篇文章 2 订阅

D. Appleman and Tree
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 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 to 1, vertex iis 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

题目大意:


给出N个点的一棵树,每个节点都有一个颜色(0/1),我们现在可以将这棵树割分成若干个联通块,但是要求每个联通块中必须有且只有一个颜色1的点,问有多少种割分方式。。


思路:


设定Dp【i】【2】:

①Dp【i】【0】表示以点i为根的子树形成割分情况的方案数,且点i连通部分没有颜色1的点。

②Dp【i】【0】表示以点i为根的子树形成割分情况的方案数,且点i连通部分有颜色1的点。


问题关键点是在于兄弟节点之间的并 ,那么有:

①Dp【i】【1】=Dp【i】【1】*(Dp【v】【0】+Dp【v】【1】)+Dp【i】【0】*Dp【v】【1】;

②Dp【i】【0】=Dp【i】【0】*(Dp【v】【0】+Dp【v】【1】);


过程维护一下即可。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
using namespace std;
int n;
vector<int>mp[150000];
int color[150000];
__int64 dp[150000][2];
__int64 mod=1e9+7;
void Dfs(int u,int from)
{
    if(color[u]==1)dp[u][1]=1;
    else dp[u][0]=1;
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        if(v==from)continue;
        Dfs(v,u);
        dp[u][1]=((dp[u][1]*(((dp[v][0]+dp[v][1]))%mod))%mod+(dp[u][0]*dp[v][1])%mod)%mod;
        dp[u][0]=(dp[u][0]*((dp[v][0]+dp[v][1]))%mod)%mod;
    }
}
int main()
{
    while(~scanf("%d",&n))
    {
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)mp[i].clear();
        for(int i=2;i<=n;i++)
        {
            int x;scanf("%d",&x);
            x++;
            mp[x].push_back(i);
            mp[i].push_back(x);
        }
        for(int i=1;i<=n;i++)scanf("%d",&color[i]);
        Dfs(1,-1);
        printf("%I64d\n",dp[1][1]%mod);
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值