HDU -- 1520 -- tree dp <deep search is written>

Source: Internet
Author: User

I certainly haven't understood the tree-like DP yet... why does it feel like there is an extra deep search for the state transition equation? Or is this because tree-like DP relies on the tree-like data structure?

This topic is regarded by many people as a tree-like DP entry question indeed ....

If u is the precursor of V, that is, the parent

DP [u] [0] + = max (DP [v] [1], DP [v] [0]) 0 indicates that u is not included in 1

DP [U [[1] + = DP [v] [0]

Not hard to write.

The root node must be located for each tree-like DP.

Because the data is very loose, I directly use vector to write.

 1 #include <iostream> 2 #include <cstring> 3 #include <vector> 4 #include <algorithm> 5 using namespace std; 6  7 const int size = 6010; 8 vector<int>ve[size]; 9 int val[size];10 int dp[size][2];11 int in[size];12 13 void dfs( int u )14 {15     int Size , v;16     Size = ve[u].size();17     dp[u][1] = val[u];18     for( int i = 0 ; i<Size ; i++ )19     {20         v = ve[u][i];21         dfs(v);22         dp[u][0] += max( dp[v][0] , dp[v][1] );23         dp[u][1] += dp[v][0];24     }25 }26 27 int main()28 {29     cin.sync_with_stdio(false);30     int n , x , y , z;31     while( cin >> n )32     {33         memset( dp , 0 , sizeof(dp) );34         memset( in , 0 , sizeof(in) );35         for( int i = 1 ; i<=n ; i++ )36         {37             cin >> val[i];38             ve[i].clear();39         }40         while(1)41         {42             cin >> x >> y;43             ve[y].push_back(x);44             in[x] ++;45             if( !x && !y )46                 break;47         }48         for( int i =1 ; i<=n ; i++ )49         {50             if( !in[i] )51             {52                 z = i;53                 break;54             }55         }    56         dfs(z);57         cout << max( dp[z][0] , dp[z][1] ) << endl;58     }59     return 0;60 }
View code

 

Today:

Hear something

It is irrelevant to you.

But I can think of you with a few turns.

  

 

HDU -- 1520 -- tree dp <deep search is written>

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.