codeforces-461b Appleman and tree--tree DP problem __ Search timeout? D

Source: Internet
Author: User
the love story of the Apple Man and the tree

Apple Man and tree love story topic description-english Approximate question analysis-bare tree dp Count dp-State definition dp-State transfer Equation code-you group of hands don't always think about copying.

@ Topic Description @-english

Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are 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 would split into (k + 1) parts. Note, which is a tree with colored vertices.
Now Appleman wonders, what are the number of sets splitting the tree in such a way this each resulting part would have exact Ly one black vertex? Find this number modulo 1000000007 (10^9 + 7).

Input
The contains an integer n (2≤n≤10^5)-the number of tree vertices.
The second line contains the description of the tree:n-1 integers, p0, ..., p1 (pn-2). Where Pi means that there is a 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 (xi are X1 0 or 1). If Xi is equal to 1, vertex I are colored black. Otherwise, vertex i is colored white.

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

Some Example

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
@ roughly

Appleman has a tree containing n (2≤n≤10^5) vertices, some of which are black. Appleman have nothing to do, want to cut off a few edges, so that the remaining connected blocks each contains a black dot (obviously cut off the edge should be the number of black nodes-1). The vertex number is 0~n-1.
input n; input n-1 number, the first Number pi table vertex i and PI connectivity, and then input n number, in turn, the color of each vertex, 0 white 1 black.
Let you seek to meet the Appleman desire of the scheme of the number of modules 10^9 + 7. @ Analysis-Bare tree dp count @

There are trees, to the statistical scheme, as the title shows. @dp-Status definition @


Now to figure out how to define the state. As the picture shows a tree with a root, imagine dividing it into several connected blocks, which is the most special one. The is obviously the connected block that contains the root node. The contains a black dot in the connected block of the root node that has three sources: its subtree, its own, its father above "if it has a father". If it is more than its father, it can be handed over to its father recursively. If it is itself, then its subtree is either disconnected or self-reliant, or connected to the white-washing ego. If on its subtree, a Shang tree is to feed the parent node.
Then we define the two-dimensional state: dp[i][2] D p [i] [2] dp[i][2]
Dp[i][0]: After the subtree with I is cut off, the total number of connected blocks containing I that do not contain black dots d p [i] [0]: a son of the root of I When the tree is cut, the total number of connected blocks containing I without black dots dp[i][0]: The total number of solutions with no black spots in the connected block with I after cutting the edge of the subtree with I as root
Dp[i][1]: After the subtree with I is cut, the connected block containing I has a black dot

Related Article

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.