Title Link: http://poj.org/problem?id=3659
Give you a tree chart, a dot can cover the points around him, so that you cover all points with the fewest points.
Dp[i][0] means overwrite with I point, dp[i][1] means overwrite with child node, dp[i][2] means overwrite with parent node
(1) dp[i][0] = min (dp[i.son][0], dp[i.son][1], dp[i.son][2])
(2) dp[i][1] = min (dp[i.son][0], dp[i.son][1])//Special award
(3) dp[i][2] = min (dp[i.son][0], dp[i.son][1])
Note (2) that because I needs to be overwritten, dp[i.son][0] needs to take at least one.
1 //#pragma COMMENT (linker, "/stack:102400000, 102400000")2#include <algorithm>3#include <iostream>4#include <cstdlib>5#include <cstring>6#include <cstdio>7#include <vector>8#include <cmath>9#include <ctime>Ten#include <list> One#include <Set> A#include <map> - using namespacestd; -typedefLong LongLL; thetypedef pair <int,int>P; - Const intN = 1e4 +5; - intdp[n][3]; - structEdge { + intnext, to; -}edge[n <<1]; + intHead[n], CNT; A atInlinevoidAddintUintv) { -Edge[cnt].next =Head[u]; -Edge[cnt].to =v; -Head[u] = cnt++; - } - in voidDfsintUintp) { -dp[u][0] =1, dp[u][1] = dp[u][2] =0; to BOOLFlag =false; + intMin =10000; - for(inti = Head[u]; ~i; i =Edge[i].next) { the intv =edge[i].to; * if(v = =p) $ Continue;Panax Notoginseng Dfs (v, u); -dp[u][0] + = min (dp[v][0], Min (dp[v][1], dp[v][2])); thedp[u][2] + = min (dp[v][1], dp[u][0]); + if(dp[v][0] <= dp[v][1]) { AFlag =true; thedp[u][1] + = dp[v][0]; +}Else { -dp[u][1] + = dp[v][1]; $min = min (dp[v][0]-dp[v][1], Min); $ } - } - if(!flag)//If you don't get dp[v][0] thedp[u][1] +=Min; - }Wuyi the intMain () - { Wu intN, u, v; - while(~SCANF ("%d", &N)) { AboutMemset (Head,-1,sizeof(head)); $CNT =0; - for(inti =1; I < n; ++i) { -scanf"%d%d", &u, &v); - Add (U, v); A Add (V, u); + } theDfs1, -1); -printf"%d\n", Min (dp[1][0], dp[1][1])); $ } the return 0; the}
POJ 3659 Cell Phone Network (tree dp)