[Tree DP] poj 1463

Source: Internet
Author: User

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

Related Article

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.