[Solution] no question of party boss

 Description

 There is a company to hold a party.
 In order to have a good time, company leaders decided: If you invite someone, then we will not invite his boss
 (boss's boss, boss's boss's boss ...... can invite).
 Everyone can add to the party atmosphere for the party number, seeking an invitation program, and the maximum value of the atmosphere.

 

 Input

 Line 1 integer N (1 <= N <= 6000) represented by the number of companies.
 Next N lines of an integer. I represents the number of the i-th row personal atmosphere value x (-128 <= x <= 127).
 Next each row two integers L, K. K represents personal L is the first individual supervisor.
 Enter at the end of 00.

 Output

 A number, maximum value and atmosphere.

 Sample Input

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

 Sample Output

5

 Problem-solving ideas

[DP classic tree questions] dp [i] [0] dp [i] [1] represents taking the maximum value i or not.

    dp【i】【0】+=max(dp【son】【1】,dp【son】【0】);

    dp【i】【1】+=max(dp【son】【0】);

 Hint

#include<bits/stdc++.h>
using namespace std;
int n,cnt,ans;
int s[1000000],v[1000000];
vector<int> tot[100000];
int dp[100000][10];
void f(int x){
    dp[x][0]=0;
    dp[x][1]=s[x];
    for(int i=0;i<tot[x].size();i++){
        int y=tot[x][i];
        f(y);
        dp[x][0]+=max(dp[y][0],dp[y][1]);
        dp[x][1]+=dp[y][0];
    }
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>s[i];
    }
    for(int i=1;i<=n-1;i++){
        int a,b;
        cin>>a>>b;
        tot[b].push_back(a);
        v[a]=1;
    }
    for(int i=1;i<=n;i++){
        if(!v[i]){
            cnt=i;
            break;
        }
    }
    f(cnt);
    ans=max(dp[cnt][0],dp[cnt][1]);
    cout<<ans;
    return 0;
}

Guess you like

Origin www.cnblogs.com/rebirth-death2019/p/12152366.html