Codeforces Round #263 (Div. 2) D. Appleman and trees (tree DP)

Source: Internet
Author: User

Topic links

D. Appleman and Treetime limit per test:2 secondsmemory limit per test:256 megabytesinput:standard Inputoutput:standard 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 Sub class= "Lower-index" > 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).

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

Test instructions: Dyeing each node, white or black, ask you to disconnect certain edges, so that each unicom block happens to have just one node when black, ask how many kinds of broken edges.

Idea: The tree dp, Dp[i][0] represents to I at this point where the subtree has only one black spot, dp[i][0] contains the number of cases where the I node has no black spots.

For each node I, calculate to one of its subtrees (Root node u) (The connecting Edge is edge), Dp[i][0] is dp[i][0] * dp[u][1] + dp[i][0] * dp[u][0], has been processed must take dp[i][0], if the edge is Subtree Fetch dp[u][0], if not take edge, then subtree takes dp[u][1].

DP[I][1] for Dp[i][1] * (dp[u][0] + dp[u][1]) + dp[i][0] *dp[u][1], if the processing finished taking Dp[i][1],edge take the words of dp[u][0], not to take the words for dp[u][1]; If the finished fetch dp[i][0], edge must be taken and multiplied by dp[u][1] (ps:dp[vu][0) can not be, if you want to the part of the U-point will appear without black spots of the case)

1#include <stdio.h>2#include <string.h>3#include <iostream>4 #defineMoD 10000000075 6 using namespacestd;7 8 structnode9 {Ten     intu; One     intv; A     intNext; -}p[100010]; - intcnt,head[100010],color[100010] ; the Long Longdp[100010][2] ; -  - voidAddedge (intUintv) - { +P[CNT].U =u; -P[CNT].V =v; +P[cnt].next =Head[u]; AHead[u] = cnt + + ; at } - voidDFS (intu) - { -Dp[u][color[u]] =1 ; -      for(inti = Head[u]; i+1; i =p[i].next) -     { in         intv =p[i].v; - DFS (v); todp[u][1] = ((dp[u][1] * dp[v][0])% mod + (dp[u][1] * dp[v][1])% mod + (dp[u][0] * dp[v][1]% MoD)%MoD; +dp[u][0] = ((dp[u][0] * dp[v][0])% mod + (dp[u][0] * dp[v][1]% MoD)%MoD; -     } the } * intMain () $ {Panax Notoginseng     intN, A; -      while(~SCANF ("%d",&N)) the     { +CNT =0 ; Amemset (head,-1,sizeof(head)); theMemset (DP,0,sizeof(DP)); +          for(inti =1; I < n; i++) -         { $scanf"%d",&a); $ Addedge (a,i); -         } -          for(inti =0; I < n; i++) thescanf"%d",&color[i]); -DFS (0) ;Wuyiprintf"%i64d\n", dp[0][1]) ; the     } -     return 0 ; Wu}
View Code

Codeforces Round #263 (Div. 2) D. Appleman and trees (tree DP)

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.