Luo Gu P1352 no explanations party boss

Title Description

A university has N employees, numbered 1 ~ N. There are dependencies between them, that is their direct supervisor relationship is like a tree rooted to the principal, the parent node is the child node. Now there is the anniversary banquet, invited to a banquet every employee will increase a certain happiness index Ri, but then, if your boss to attend a staff party, then the staff would in any case would not come to the ball. Therefore, you programmed computing, which allows staff to invite the greatest happiness index, find the greatest happiness index.

Input Format

A first line integer N. (1 <= N <= 6000)

Next row N, i + 1-row number represents happiness index Ri i staff. (-128 <= Ri <= 127)

Then N-1 lines, each pair of input integer L, K. L represents K is the direct supervisor.

The last line of input 00

Output Format

The maximum output of the happiness index.

Sample input and output

Input # 1
7
1
1
1
1
1
1
1
1 3
2 3
6 4
7 4
4 5
3 5
0 0
Output # 1
5 

Resolution:

Tree-DP, set vector array son [i] is the son of a collection point i
dp [i] [1] This figure represents invited i
dp [i] [0] i said they did not invite people
to invite Father and son meaning of the questions can not be to
the state transition equation:
DP [the root] [0] STD + :: = max (DP [Son [the root] [I]] [0], DP [Son [the root] [I]] [. 1])
DP [ root] [1] + = dp [son [root] [i]] [0]

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <queue>
 7 #include <stack>
 8 #include <vector>
 9 #define Max 6050
10 #define re register
11 std::vector<int>son[Max];
12 int n,root,dp[Max][2],fa[Max];
13 void dfs(int root) {
14     for(re int i = 0 ; i < son[root].size() ; ++ i)
15         dfs(son[root][i]);
16     for(re int i = 0 ; i < son[root].size() ; ++ i) {
17         dp[root][0] += std::max(dp[son[root][i]][0],dp[son[root][i]][1]);
18         dp[root][1] += dp[son[root][i]][0];
19     }
20 }
21 void init() {
22     scanf("%d",&n);int u,v;memset(fa,-1,sizeof fa);
23     for(re int i = 1 ; i <= n ; ++ i) scanf("%d",&dp[i][1]);
24     for(re int i = 1 ; i < n ; ++ i)
25         scanf("%d%d",&u,&v),fa[u]=v,son[v].push_back(u);
26     scanf("%d%d",&u,&v);
27 }
28 inline void print(int root) {printf("%d",std::max(dp[root][0],dp[root][1]));}
29 void work() {
30     int root=1;
31     while(fa[root] != -1) root = fa[root];
32     dfs(root);
33     print(root);
34 }
35 int main() {
36     init();
37     work();
38     return 0;
39 }
AC Code

Guess you like

Origin www.cnblogs.com/handsomegodzilla/p/11359388.html