[codevs1380] no boss's prom.
Question Description
Ural University has a staff of N and is numbered 1~n. They have affiliation, which means that their relationship is like a tree rooted in the headmaster, and the parent node is the direct boss of the child node. Each employee has a happiness index. There is now an anniversary party, which asks the staff to have the most happiness index. However, no staff is willing to attend with the direct boss.
Input
The first line is an integer n. (1<=n<=6000)
Next n lines, line i+1 represents the happiness index RI for staff i. ( -128<=ri<=127)
Next N-1 line, enter a pair of integer l, K for each line. Indicates that K is the direct boss of L.
Last line input 0, 0.
Output
Output the maximum happiness index.
Input example
7 1 1 1 1 1 1 1 1 3 2 3 6 4 7 4 4 5 3 5 0 0
Output example
5
Data size and conventions
See " Input "
Exercises
I'm going to brush the water!
Treat everyone as a node. Set F[0][i] indicates that for subtree I, the maximum value that can be obtained by the I node is not selected; F[1][i] indicates the maximum value that can be obtained by selecting the I node. So
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <stack > #include <vector> #include <queue> #include <cstring> #include <string> #include <map > #include <set>using namespace std;const int buffersize = 1 << 16;char buffer[buffersize], *head, *TAIL;INL ine Char Getchar () {if (Head = = Tail) {int L = fread (buffer, 1, buffersize, stdin); Tail = (Head = buffer) + L; } return *head++;} int read () {int x = 0, f = 1; char c = Getchar (); while (!isdigit (c)) {if (c = = '-') f =-1; c = Getchar ();} while (IsDigit (c)) {x = x * + C-' 0 '; c = Getchar ();} return x * f;} #define MAXN 6010#define maxm 12010int N, M, HEAD[MAXN], NEXT[MAXM], TO[MAXM], val[maxn];void addedge (int a, int b) {to[++ M] = b; NEXT[M] = Head[a]; Head[a] = M;swap (A, b); to[++m] = b; NEXT[M] = Head[a]; Head[a] = M;return;} int f[2][maxn];void DP (int u, int fa) {F[0][u] = 0; F[1][u] = val[u];for (int e = Head[u]; E e = Next[e]) if (to[e]! = FA) {DP (to[e], u), f[0][u] + = MAX (F[0][to[e]], f[1][to[e]]); F[1][u] + = F[0][to[e]];} return;} int main () {n = read (), for (int i = 1; I <= n; i++) Val[i] = read (); for (int i = 1; i < n; i++) {int a = read (), B = r EAD (); Addedge (A, b);} Read (); Read ();dp(1, 0);p rintf ("%d\n", Max (F[0][1], f[1][1])); return 0;}
Double experience: BZOJ2060, change the input.
[codevs1380] No Boss's Prom ([bzoj2060][usaco2010 nov]visiting cows visits cows)