HDU-1520-Anniversary party
Http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 1520
DFS, some nodes have a parent-child relationship, and the node with a parent-child relationship must not appear at the same time
# Include <iostream> # include <cstdio> # include <cstring> # define N 6001 struct node // The right sibling method of the left two sons creates {int parent; int child; int brother; int attend; int notattend; int Max () {return attend> notattend? Attend: notattend;} void init () {parent = 0; child = 0; brother = 0; notattend = 0 ;}} p [N]; void dfs (int x) {int child; child = p [x]. child; while (child) {dfs (child); p [x]. attend + = p [child]. notattend; p [x]. notattend + = p [child]. max (); child = p [child]. brother ;}} int main () {int I, n, a, B; while (scanf ("% d", & n )! = EOF) {for (I = 1; I <= n; I ++) {p [I]. init (); scanf ("% d", & p [I]. attend);} while (scanf ("% d", & a, & B), a | B) {p [a]. parent = B; p [a]. brother = p [B]. child; p [B]. child = a ;}for (I = 1; I <= n; I ++) if (p [I]. parent = 0) {dfs (I); printf ("% d \ n", p [I]. max (); break;} return 0 ;}