Codeforces 461B. Appleman and tree[tree DP scheme number]

Source: Internet
Author: User

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.

Consider 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(K + 1) parts. Note, that all part is a tree with colored vertices.

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 -1 integers < Span class= "Tex-span" > p 0, p 1, ..., p n -2  (0≤ p i ≤ i ). Where  p i  means that there was an edge Connecting Vertex  ( i + 1)  of the tree and vertex  p i . Consider tree vertices is numbered from 0 to  N -1.

The third line contains the description of the colors of the vertices: n integers x0, x< /c5>1, ..., xn -1 (xi is either 0 or 1). If xi was equal to 1, vertex i was colored black. Otherwise, vertex i was colored white.

Output

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

Examplesinput
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
Test instructions: Divided into a number of connected blocks, each with only one black node, the number of solutions
F[I][0/1] Indicates whether the subtree I-root is the number of scenarios in a connected block with a black node
F[u][1]= (f[u][1]* (f[v][0]+f[v][1]) +f[u][0]*f[v][1])%mod; V is 0 u connected to him, V is 1 unconnected; U is 0 o'clock to V is 1 f[u][0]=f[u][0]* (f[v][0]+f[v][1])%mod;
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespaceStd;typedefLong Longll;Const intn=1e5+5, mod=1e9+7; inlineintRead () {CharC=getchar ();intx=0, f=1;  while(c<'0'|| C>'9'){if(c=='-') f=-1; C=GetChar ();}  while(c>='0'&&c<='9') {x=x*Ten+c-'0'; C=GetChar ();} returnx*F;}structedge{intV,ne;} E[n<<1];intCnt=0, H[n],w[n];inlinevoidInsintUintv) {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;}intN;ll f[n][2];voiddpintUintFA) {    if(W[u]) f[u][1]=1; Elsef[u][0]=1;  for(intI=h[u];i;i=e[i].ne) {        intv=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; }}intMain () {n=read ();  for(intI=1; i<=n-1; i++) Ins (read (), i);  for(intI=0; i<n;i++) w[i]=read (); DP (0,-1); cout<<f[0][1];}

Codeforces 461B. Appleman and tree[tree DP scheme number]

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.