[1352] Luo Gu no 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 and output format
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 formats:

The maximum output of the happiness index.

Input Output Sample
Input Sample # 1: Copy

7

1
1
1
1
1
1
1
1 3
2 3
6 4
7 4
4 5
3 5
0 0

Output Sample # 1: Copy

5

 

(DP wrote a year ago hey hey hey)
Step1: This question is a subject of the entry-level tree-DP

Step2: The title Housewives _ _ multiple sets of data in the case of the hereinafter refer father generation direct

Step3: known meaning of the questions, father and son while removing only one transfer equation can be derived: dp [m] [1] = dp [i] [0]. I \\ m is father to represent 1, 0 indicates not \\ dp [m] [0] + = max (dp [i] [1], dp [i] [0]) i.e., if not the father , then find the maximum value in a go or not sons of the maximum benefit

Step4: first to find the top of the tree root} {ancestor, and then from the DFS ancestor

Step5: a seemingly nothing to say

Code;

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=10005;
int dp[N][5];  
int fa[N],vis[N],n;
void dfs(int m){
    vis[m]=1;//标记m已经访问 
    for(int i=1;i<=n;i++){
        if(vis[i]==0FA && [i] == m) {
             // If the access point has not led i and m is an ortholog of (father) 
            DFS (i); // DFS son son 
            DP [m] [ 0 ] + = max ( DP [i] [ 0 ], DP [i] [ . 1 ]);
             // m not, or not to take the maximum value i 
            DP [m] [ . 1 ] + DP = [i] [ 0 ];
             / / m to go, will not go to the i 
        } 
    } 
} 
void the init () { 
    Memset (DP, 0 , the sizeof (DP)); // initialize DP 
    Memset (FA, 0 , the sizeof (FA)); //初始化fa
    memset(vis,0,sizeof(vis));//初始化vis
} 
int main(){
    freopen("1352.in","r",stdin);
    freopen("1352.out","w",stdout);
    while(scanf("%d",&n)!=EOF){
        init();//初始化 
        for(int i=1;i<=n;i++)
            scanf("d% " , & dp [i] [ 1 ]);
         // dp [i] [0/1] represents the i personally would not go to the Party
         // 0 part, do not, one go, only if it go when will add value to this happy
         // so happy this value directly to the store to dp [i] [1] to 
        int the X-, the y-, RT = 1 ; // RT from the first of them began to find his father 
        the while (scanf ( " % D% D " , & x, & Y) = the EOF && x + Y>! 0 ) 
            FA [x] = Y; // Y is the father of x 
        the while (FA [RT] =! 0 ) // find the parent node point (i.e., where the next root) 
            RT = FA [RT]; // search the final result will be the root of the tree top 
        DFS (RT); //Successful search tree to the root of a top 
        int ANS = max (DP [RT] [ 0 ], DP [RT] [ . 1 ]); // number of programs selected or not selected in the maximum value of 
        the printf ( " % D \ n- " , ANS); 
    } 
    return  0 ; 
}
 

 

Guess you like

Origin www.cnblogs.com/wuhu-JJJ/p/11241676.html