DP [k] [0] indicates the minimum number of knots required for the subtree with K points as the root (K is not used). Similarly, DP [k] [1] indicates the minimum number of knots required for the subtree with K points as the root (k-Fetch). If the parent node is not retrieved, the son node is required.
There is a transfer equation:DP [root] [0] = DP [son] [1], DP [root] [0] = min (DP [son] [1], DP [son] [0]) + 1, Using DFS backtracking for DP
# Include <map> # include <set> # include <list> # include <queue> # include <deque> # include <stack> # include <string> # include <cstdio> # include <math. h> # include <iomanip> # include <cstdlib> # include <limits. h> # include <string. h> # include <iostream> # include <fstream> # include <algorithm> using namespace STD; # define ll long # define Min-99999999 # define Max 99999999 # define PII pair <int, int> # define bug cout <"He Re !! "<Endl # define PI ACOs (-1.0) # define fre freopen (" input.txt "," r ", stdin) # define FF freopen (" output.txt "," W ", stdout) # define EPS 1e-8 # define n 1505int DP [N] [2]; vector <int> V [N]; int in [N]; void Gao (INT root) {int I; for (I = 0; I <V [root]. size (); I ++) {int son = V [root] [I]; Gao (son ); DP [root] [0] + = DP [son] [1]; DP [root] [1] + = min (DP [son] [1], DP [son] [0]) ;}} int main () {int N; while (scanf ("% d", & N )! =-1) {int I, j; for (I = 0; I <= N; I ++) {v [I]. clear (); DP [I] [0] = DP [I] [1] = max; in [I] = 0 ;}for (I = 0; I <N; I ++) {int R, num; scanf ("% d :( % d)", & R, & num); While (Num --) {int; scanf ("% d", & A); V [R]. push_back (a); in [a] ++; DP [a] [0] = 0; DP [a] [1] = 1 ;}} int root; for (I = 0; I <n; I ++) if (! In [I]) root = I; // find the root node number DP [root] [0] = 0; DP [root] [1] = 1; Gao (Root ); int ans = min (DP [root] [0], DP [root] [1]); printf ("% d \ n", ANS);} return 0 ;}