Codeforces 461 B. Appleman and Tree

Source: Internet
Author: User



Tree-shaped DP ...

B. Appleman and Treetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

Appleman have a tree with n vertices. Some of the vertices (at least one) is colored black and other vertices is colored white.

Con Sider a set consisting of  K   (0?≤? K ? <? n )  edges of Appleman ' s tree. If Appleman deletes these edges from the tree and then it'll split Into 

Now Appleman wonders, what's the number of sets splitting the tree in such a-the-the-each-resulting part would have exact Ly one black vertex? Find this number modulo 1000000007 (9? +?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?-? 1Integers P0,? P 1,?...,? P n?-? 2 (0?≤? P i? ≤? I ). Where Pi means that there was an edge connecting vertex(i. +?1)of the tree and vertex Pi . Consider tree vertices is numbered from0To n?-? 1.

The third line contains the description of the colors of the vertices:NIntegers x0,? x 1,?...,? x n?-? 1 ( xi is either0Or1). If xi is equal to1, vertexIis colored black. Otherwise, VertexIis colored white.

Output

Output a single integer-the number of ways to split the tree modulo 1000000007 (9? +?7).

Sample Test (s) 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

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace Std;const int maxn=100010;const Long long int mod=1000000007ll;int n;struct edge{int to,next;} Edge[3*maxn];int adj[maxn],size;void init () {Size=0;memset (adj,-1,sizeof (ADJ));} void Add_edge (int u,int v) {Edge[size].to=v;edge[size].next=adj[u]; adj[u]=size++;} int c[maxn];long long int dp[maxn][2];void dfs (int u,int fa) {dp[u][c[u]]=1;for (int i=adj[u];~i;i=edge[i].next) {int v= Edge[i].to;if (V==FA) Continue;dfs (v,u);DP [u][1]= (dp[u][1]*dp[v][1]%mod+dp[u][0]*dp[v][1]%mod+dp[u][1]*dp[v][0]% MoD)%mod;dp[u][0]= (dp[u][0]*dp[v][0]%mod+dp[u][0]*dp[v][1]%mod)%mod;}} int main () {init (); scanf ("%d", &n), for (int i=1;i<n;i++) {int v;scanf ("%d", &v); Add_edge (I,V); Add_edge (v,i);} for (int i=0;i<n;i++) scanf ("%d", C+i);d FS (0,0);p rintf ("%i64d\n", dp[0][1]); return 0;}




Codeforces 461 B. Appleman and Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.