Codeforces 461B Appleman and Tree:tree DP

Source: Internet
Author: User

Title Link: Http://codeforces.com/problemset/problem/461/B

Test instructions

Give you a tree (numbered from 0 to n-1,0 as the root node), each node has a black and white color, where the black node has k+1.

Now let's delete the K-bar, make the tree into a k+1, and make sure that there is only one black node in each connected block.

Ask you how many ways to cut the edge.

Exercises

Indicates the state:

DP[I][0/1] = Numbers

Represents the number of delete methods for the subtree of node I, when there are (1) or no (0) black nodes in the connected block where node I is located

Because it is always necessary to ensure that there is only one black point in each connected block, you must delete the exact K-bar at the end, without recording how many edges are currently deleted.

Find the Answer:

Ans = dp[0][1]

There must be only one black point in the connected block where the final root is located.

How to transfer:

The process of deleting edges is considered in turn.

Delete node I from the side of its son, equivalent to:

I do not have a son, and then add a subtrees tree for its son, while guaranteeing the legitimacy.

Then the final solution depends on three conditions:

(1) I where the connected block (for short, block I) whether there are black spots

(2) If there is a black point in the connected block (called Block son) where son is located

(3) whether to delete the Edge (I,son)

Sub-situation discussion:

(1) Block I with black dots

A. The block son has black spots, at this time can only delete the edge, the final block I have black dots

B. Block son is all white, at this time can only retain this edge, the final block I have black dots

(2) I is white

A. The block son has black spots, at this time to delete the edge or not delete can:

I. Delete, the final block I is all white

II. Do not delete, the final block I have black dots

B. Block son is all white, at this time can only retain this edge, the final block I is all white

Comprehensive:

DP[NOW][1] = dp[son][0]*dp[now][1] + dp[son][1]* (dp[now][1]+dp[now][0])

Dp[now][0] = (dp[son][0]+dp[son][1]) *dp[now][0]

Boundary conditions:

Dp[i][c[i]]=1

AC Code:

#include <iostream>#include<stdio.h>#include<string.h>#include<vector>#defineMax_n 100005#defineMOD 1000000007using namespacestd;intN;intC[max_n];Long Longdp[max_n][2];vector<int>Edge[max_n];voidRead () {cin>>N; intx;  for(intI=1; i<n;i++) {cin>>x;    Edge[x].push_back (i); }     for(intI=0; i<n;i++) {cin>>C[i]; }}voidDfsintNow ) {Dp[now][c[now]]=1;  for(intI=0; I<edge[now].size (); i++)    {        inttemp=Edge[now][i];        DFS (temp); Long Longblk=dp[now][1]; Long Longwht=dp[now][0]; dp[now][1]= (dp[temp][0]*blk+dp[temp][1]* (BLK+WHT))%MOD; dp[now][0]= (dp[temp][0]+dp[temp][1]) *wht%MOD; }}voidWork () {DFS (0); cout<<dp[0][1]<<Endl;}intMain () {read (); Work ();}

Codeforces 461B Appleman and Tree: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.