Codeforces 461B. Appleman and Tree[树形DP 方案数]

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

30 00 1 1

output

2

input

60 1 1 0 41 1 0 0 1 0

output

1

input

100 1 2 1 4 4 4 0 80 0 0 1 0 1 1 0 0 1

output

27

题意:分成若干个连通块,每个只有一个黑色节点,求方案数

f[i][0/1]表示以i为根的子树i是否在有黑色节点的连通块中的方案数
f[u][1]=(f[u][1]*(f[v][0]+f[v][1])+f[u][0]*f[v][1])%MOD; v是0 u跟他相连,v是1 不相连;u是0时要跟v是1相连
f[u][0]=f[u][0]*(f[v][0]+f[v][1])%MOD;同理
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int N=1e5+5,MOD=1e9+7;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1; c=getchar();}
    while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘; c=getchar();}
    return x*f;
}
struct edge{
    int v,ne;
}e[N<<1];
int cnt=0,h[N],w[N];
inline void ins(int u,int v){
    cnt++;
    e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt;
    cnt++;
    e[cnt].v=u;e[cnt].ne=h[v];h[v]=cnt;
}
int n;ll f[N][2];
void dp(int u,int fa){
    if(w[u]) f[u][1]=1;
    else f[u][0]=1;
    for(int i=h[u];i;i=e[i].ne){
        int v=e[i].v;
        if(v==fa) continue;
        dp(v,u);
        f[u][1]=(f[u][1]*(f[v][0]+f[v][1])+f[u][0]*f[v][1])%MOD;
        f[u][0]=f[u][0]*(f[v][0]+f[v][1])%MOD;
    }
}
int main(){
    n=read();
    for(int i=1;i<=n-1;i++) ins(read(),i);
    for(int i=0;i<n;i++) w[i]=read();
    dp(0,-1);
    cout<<f[0][1];
}
时间: 2024-10-19 19:47:39

Codeforces 461B. Appleman and Tree[树形DP 方案数]的相关文章

Codeforces 461B Appleman and Tree(树形dp)

题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每个节点的父亲节点,以及每个点的颜色(0表示白色,1表示黑色),切断这棵树的k条边,使得树变成k+1个联通分量,保证每个联通分量有且仅有1个黑色节点.问有多少种分割方法. 解题思路:树形dp,dp[i][0]和dp[i][1]分别表示子树一下的分割方法中,i节点所在联通块不存在黑节点和已经存在一个黑节点的方案数. #include <cstdio> #include <c

Codeforces 461B Appleman and Tree(木dp)

题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k条边,使得树变成k+1个联通分量.保证每一个联通分量有且仅有1个黑色节点.问有多少种切割方法. 解题思路:树形dp,dp[i][0]和dp[i][1]分别表示子树一下的切割方法中,i节点所在联通块不存在黑节点和已经存在一个黑节点的方案数. #include <cstdio> #include &l

CF 461B Appleman and Tree 树形DP

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

Codeforces 462D Appleman and Tree 树形dp

题目链接:点击打开链接 题意: 给定n个点的树, 0为根,下面n-1行表示每个点的父节点 最后一行n个数 表示每个点的颜色,0为白色,1为黑色. 把树分成若干个联通块使得每个联通块有且仅有一个黑点,问有多少种分法(结果mod1e9+7) 思路: 树形dp,每个点有2个状态,已经归属于某个黑点和未归属于某个黑点. #include <cstdio> #include <vector> #include <iostream> using namespace std; #de

codeforces 416B. Appleman and Tree 树形dp

题目链接 Fill a DP table such as the following bottom-up: DP[v][0] = the number of ways that the subtree rooted at vertex v has no black vertex. DP[v][1] = the number of ways that the subtree rooted at vertex v has one black vertex. The recursion pseudo

【树形DP】CODEFORCES 461B Appleman and Tree

通道 题意:一棵树上有K个黑色节点,剩余节点都为白色,将其划分成K个子树,使得每棵树上都只有1个黑色节点,共有多少种划分方案 思路:dp[i][0]和dp[i][1]分别表示i子树所在联通块不存在黑节点和已经存在一个黑节点的方案数 代码: #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #include<ma

Codeforces Round #263 (Div. 2) D. Appleman and Tree 树形dp

链接: http://codeforces.com/contest/462/problem/D 题意: 给定n个点的树, 0为根,下面n-1行表示每个点的父节点 最后一行n个数 表示每个点的颜色,0为白色,1为黑色. 把树分成若干个联通块使得每个联通块有且仅有一个黑点,问有多少种分法(结果mod1e9+7) 题解: 树形dp,每个点有2个状态,已经归属于某个黑点和未归属于某个黑点. 代码: 31 int n; 32 int x[MAXN]; 33 VI G[MAXN]; 34 ll dp[MAX

Codeforces 461B - Appleman and Tree 树状DP

一棵树上有K个黑色节点,剩余节点都为白色,将其划分成K个子树,使得每棵树上都只有1个黑色节点,共有多少种划分方案. 个人感觉这题比较难.假设dp(i,0..1)代表的是以i为根节点的子树种有0..1个黑色节点的划分方案数. 当节点i为白色时,对于它的每个孩子的节点处理: 求dp(i, 0)时有: 1,将该节点与孩子节点相连,但要保证孩子节点所在的子树种没有黑色节点: 2,将该节点不与该孩子节点相连,则该孩子节点要保证所在子树种有黑色节点: 即dp(i, 0) = π(dp(j,0 ) + dp(

codeforces 461B Appleman and Tree

题意:问你将含有黑白点的无向树使得每个子树中只有一个黑点的方法数. 解题思路:树形dp,dp[i][0/1] 表示 第i 个节点的联通图中是否有 1个黑点的种类数. 解题代码: 1 // File Name: 461c.cpp 2 // Author: darkdream 3 // Created Time: 2015年03月11日 星期三 10时53分22秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8