HDU 1520 Anniversary Party, hduanniversary
Question here: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 1520
Question. This is my memo and there is no comment.
1 # include <iostream> 2 # include <vector> 3 # include <cstring> 4 5 using namespace std; 6 7 8/* 9 dp [I] [1] = max (dp [I parent] [0] + score [I]) 10 dp [I] [0] = max (dp [I parent] [1], dp [I parent] [0]) 11 12 13 if I de: for each child node j, j cannot go to 14 dp [I] [1] = sum (dp [j] [0]) + score [I]; 15 else I does not go: for each of its child node j, j can not go, select a scheme with a large happy value 16 dp [I] [0] = sum (max (dp [j] [0], dp [j] [1]); 17 */18 19 const int MAX_N = 6000; 20 int SC Ore [MAX_N + 1]; 21 std: vector <int> graph (MAX_N + 1, std: vector <int> (0, 0); 22 int parent [MAX_N + 1]; 23 24 int n; 25 int dp [MAX_N + 1] [2]; 26 27 void treeDP (int node) 28 {29 dp [node] [1] = score [node]; 30 dp [node] [0] = 0; 31 32 for (int I = 0; I <graph [node]. size (); ++ I) 33 treeDP (graph [node] [I]); 34 35 for (int I = 0; I <graph [node]. size (); ++ I) 36 {37 int child = graph [node] [I]; 38 39 dp [Node] [1] + = dp [child] [0]; 40 dp [node] [0] + = max (dp [child] [0], dp [child] [1]); 41} 42} 43 44 int main (int argc, char const * argv []) 45 {46 while (cin> n) 47 {48 memset (score, 0, sizeof (int) * (MAX_N + 1); 49 memset (parent, 0, sizeof (int) * (MAX_N + 1 )); 50 memset (dp, 0, sizeof (int) * (MAX_N + 1) * 2); 51 for (int I = 1; I <= n; ++ I) 52 {53 cin> score [I]; 54 graph [I]. clear (); 55} 56 57 int l, k; 58 while (True) 59 {60 cin> l> k; 61 62 if (l = 0 & k = 0) 63 break; 64 else65 {parent [l] = k; 66 graph [k]. push_back (l); 67} 68} 69 70 // find root node71 int root = 1; 72 while (parent [root]! = 0) 73 root = parent [root]; 74 75 treeDP (root); 76 77 cout <max (dp [root] [0], dp [root] [1]) <endl; 78} 79 return 0; 80}