Hdu1054 strategic game (tree DP)

Source: Internet
Author: User

Hey, my first tree-like DP

Cover the entire tree with the least number of points. Let's take a look at the sample input and you will know the meaning of the question.

Analysis:

Make a dynamic plan on the tree

[Status ]:

DP [I] [0] is the root node based on I, and this node is not placed, the minimum number of points required

DP [I] [1] is the root node with I as the minimum number of points required for this node to be placed.

 

[Transfer equation ]:

DP [I] [0] = sum (DP [son [I] [J] [1]) if this point is not put, then all of its son nodes must be put, in this way, the edge can be overwritten.

DP [I] [1] = sum (min (DP [son [I] [J] [0], DP [son [I] [J] [1]) If this point is put, its son node has two kinds of decision-making: put, one is not to put it, just take Min.

 
# Include <iostream> using namespace STD; int DP [1505] [2], F [1505], ANS, N; int son [1505] [1505], size [1505];
Int DFS (INT POs, int Val) {If (DP [POS] [Val]! = Int_min) return DP [POS] [Val]; int sum = val; For (INT I = 0; I <size [POS]; I ++) {If (val = 1) sum + = min (DFS (son [POS] [I], 0), DFS (son [POS] [I], 1 )); else sum + = DFS (son [POS] [I], 1); // the current node is not selected, then the subnode must select} return DP [POS] [Val] = sum;} int main () {While (scanf ("% d", & n) = 1) {for (INT I = 0; I <n; I ++) {f [I] = I; DP [I] [1] = DP [I] [0] = int_min;} For (INT I = 0; I <n; I ++) {int X, M; scanf ("% d :( % d)", & X, & M); Size [x] = m; For (Int J = 0; j <size [x]; j ++) {scanf ("% d", & Son [x] [J]); F [son [x] [J] = x ;}} for (INT I = 0; I <n; I ++) {If (F [I] = I) {ans = min (DFS (I, 0 ), DFS (I, 1); break;} printf ("% d \ n", ANS);} return 0 ;}

 

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.