[ZJOI2006] tricolor binary tree (tree dp)

 

A turn talking about very detailed explanations ↓ clear and concise, to kneel orz


Solution to a problem P2585 [[ZJOI2006] tricolor binary tree]

Fading . Posted ON 2018-03-17 11:40:53 | an under tree Dynamic Programming | 19 


Dp equation bigwigs did not explain ah ...

Dp is my recursive implementation,Unlike bigwigs like a tree with dfs

------Dividing line------

First achievements.

This tree is a recursive definition, so consider using dfs achievements.

Sample said it. 1122002010

Adjacency list of what does not exist! Because it is a binary tree, so we use an adjacency matrix will be more convenient.

Provided Tree [i] [ 0 ] for the i-th node of the left node number 
provided Tree [i] [ . 1 ] for the i-th node to the right node number 

void DFS ( int the root) { // the root represents the number of several nodes 
    tot ++; // represents the total visited several nodes. 
    IF (S [the root] == ' 0 ' ) return ; // leaf node exits 
    IF (S [the root] == ' . 1 ' ) { 
        Tree [the root] [ 0 ] = the root + . 1 ; // next access node this number must be the node number + 1'd 
        DFS (the root + . 1 ); // search downwardly 
    }
     IF (S [the root] ==' 2 ' ) { 
        Tree [the root] [ 0 ] = the root + . 1 ; 
        DFS (the root + . 1 ); 
        Tree [the root] [ . 1 ] = TOT + . 1 ; // right node must have total access number + 1'd 
        DFS (TOT + . 1 ); 
    } 
}

 

Then guide dp equation

First seek the maximum. This staining problem is rather disgusting. In the process of dp, we do not know whether it's around the junction dyed green,

Therefore dp that the results have a problem? ? ?

So we can consider what the state record of dp.

This method is not the boss of me dance learned from the P1352, we can look at.

Set F [i] [ 0 ] indicates the i-th points, not stained green, which sub-tree (including themselves) dyed green maximum value. 
Set F [i] [ . 1 ] denotes the i-th point, a green dye, which sub-tree (including themselves) dyed green maximum value. 
Well, first of all, if the i-th point dyed green, then left and right node can not be dyed green. So consider about the state of the node is not dyed green values are added, they will have the results. 
So f [i] [ 1 ] = f [Tree [i] [ 0 ]] [ 0 ] + f [Tree [i] [ 1 ] [ 0 ]] 
if this point does not dye it green? 
We think, this point is not dyed green, then the meaning of the questions, it's about the same time can not be dyed green node, two-node enumeration staining on it. 
We find that the maximum value, then F [I] [ 0 ] is not max (left node dyed green value + right node not stained green value, a green dye left node value + right node value is not dyed green, left the value of the node is not dyed green + green value) right node is not stained? 
The problem is resolved, then the minimum value is the same. Sahua! ! !

...... There are so easily? ? ?

This question pits come, not dyed green for the case of the i-node, two nodes are not dyed green case does not exist.

why? ? ?

If the i-th point and its children are not dyed green, then three points can only be infected with red, blue and white.

However, according to the principles of the drawer, the two colors infected three points, two points must have the same color! ! !

So, do not meet the meaning of the questions, tan90! nonexistent! That's why it's called tri-color binary tree.

Finally, start dp

Why use dfs? Why use dfs?

Since the subtree for which some sort of number root <left node <right node

Therefore, the use recursion, nothing wrong ah.

From start to push down the number of nodes on it.

for (int i=n;i>=1;i--){
    f[i][1]=f[tree[i][0]][0]+f[tree[i][1]][0]+1;
    f[i][0]=max(f[tree[i][0]][1]+f[tree[i][1]][0],f[tree[i][0]][0]+f[tree[i][1]][1]);
}
printf("%d ",max(f[1][1],f[1][0 ])); // Finally, we have only to compare unstained and stained roots which better answer 
for ( int I = n-; I> = . 1 ; i-- ) { 
    F [I] [ . 1 ] = F [Tree [I] [ 0 ]] [ 0 ] + F [Tree [I] [ . 1 ]] [ 0 ] + . 1 ; 
    F [I] [ 0 ] = min (F [Tree [I] [ 0 ]] [ . 1 ] + F [Tree [I] [ . 1 ]] [ 0 ], F [Tree [I] [ 0 ]] [ 0 ] + F [Tree [I] [ . 1 ]] [ . 1 ]); 
} 
the printf ( " D% " , min (F [1 ] [ 1 ], f [ 1 ] [ 0 ])); // Finally, we have only to compare the roots dyed and not dyed answers which better

 

True Sahua


Make up a complete code
 

 

Guess you like

Origin www.cnblogs.com/phemiku/p/11430575.html