当前位置:网站首页>(tree DP) acwing 285 A dance without a boss

(tree DP) acwing 285 A dance without a boss

2022-06-13 09:23:00 Age worry

285. A dance without a boss

Topic link https://www.acwing.com/problem/content/287/
subject :
 Insert picture description here
 Insert picture description here


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
const int N=6010;
int w[N];
vector<int > q[N];
bool fa[N];
int f[N][2];
void dfs(int u){
    
    f[u][1]=w[u];
    for(int i=0;i<q[u].size();i++){
    
        int j=q[u][i];
        dfs(j);
        f[u][1]+=f[j][0];
        f[u][0]+=max(f[j][0],f[j][1]);
    }

}
int main(){
    
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
        scanf("%d",&w[i]);
    int x,y;
    for(int i=1;i<n;i++){
    
        scanf("%d%d",&x,&y);
        q[y].push_back(x);
        fa[x]=1;
    }
    int t=1;
    while(fa[t]) t++;
    dfs(t);
    printf("%d\n",max(f[t][0],f[t][1]));
    return 0;
}

原网站

版权声明
本文为[Age worry]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270532192848.html