P1352 no boss dance, P1352 boss dance
Description
A university has N employees ranging from 1 ~ N. There is a subordination between them, that is, their relationship is like a tree with the principal as the root, and the parent node is the direct supervisor of the child node. There is now a Celebration Banquet, and each invitation to a banquet will increase the happiness index Ri. However, if the boss of a staff member comes to the dance, the employee will not come to the dance in any way. Therefore, please Program Computing and invite employees who can maximize the happiness index and find the greatest happiness index.
Input/Output Format
Input Format:
The first line is an integer N. (1 <= N <= 6000)
In the next N rows, row I + 1 indicates the happiness index Ri of employee I. (-128 <= Ri <= 127)
Next to the N-1 line, enter a pair of integers L, K in each line. Indicates that K is the direct supervisor of L.
Enter 0 in the last row.
Output Format:
Output the largest happiness index.
Input and Output sample input sample #1:
711111111 32 36 47 44 53 50 0
We use dp [I] [0] to indicate that this point is not attended.
Use dp [I] [1] to represent this point
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <queue> 6 using namespace std; 7 const int MAXN = 6001; 8 void read (int & n) 9 {10 char c = '+'; int x = 0; bool flag = 0; 11 while (c <'0' | c> '9') 12 {c = getchar (); if (c = '-') flag = 1 ;} 13 while (c> = '0' & c <= '9') 14 {x = x * 10 + (c-48); c = getchar ();} 15 flag = 1? N =-x: n = x; 16} 17 int n; 18 int ha [MAXN]; 19 int fa [MAXN]; 20 int ch [MAXN]; 21 int dp [MAXN] [3]; 22 int deg [MAXN]; 23 int ans = 0; 24 void Topsort () 25 {26 queue <int> q; 27 for (int I = 1; I <= n; I ++) 28 if (deg [I] = 0) 29 q. push (I); 30 while (q. size ()! = 0) 31 {32 int p = q. front (); 33 q. pop (); 34 deg [fa [p] --; 35 if (deg [fa [p] = 0 & fa [p]! =-1) 36 q. push (fa [p]); 37 dp [fa [p] [0] + = max (dp [p] [0], dp [p] [1]); 38 // The supervisor does not participate in 39 dp [fa [p] [1] + = dp [p] [0]; 40 // participate in 41 if (fa [p] =-1) 42 ans + = max (dp [fa [p] [1], dp [fa [p] [0]); 43} 44 printf ("% d", ans); 45 46} 47 int main () 48 {49 read (n); 50 memset (ch,-1, sizeof (ch); 51 memset (fa,-1, sizeof (fa )); 52 for (int I = 1; I <= n; I ++) 53 read (ha [I]); 54 for (int I = 1; I <= n; I ++) dp [I] [1] = ha [I]; 55 for (int I = 1; I <= n; I ++) 56 {57 int x, y; 58 read (x); read (y); 59 fa [x] = y; 60 ch [y] = x; 61 deg [y] ++; 62} 63 Topsort (); 64 return 0; 65}