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 ;}