acwing 285 没有上司的舞会 (tupo)

没有上司的舞会

#include <iostream>
#include <queue>
#include <algorithm>
#include <cstring>


using namespace std;

const int N = 6010;

int happy[N], f[N], dp[N][2], d[N], n, root;

bool vis[N];

void tupo()
{
    queue<int> q;
    for(int i = 1;i <= n ;i ++)
    {
        if(!d[i]) 
        {
            q.push(i);
            vis[i] = true;
        }
    }
    while(q.size())
    {
        int t = q.front();  
        q.pop();
        
        root = t; 
        dp[f[t]][0] += max(dp[t][1] + happy[t], dp[t][0]);
        
        dp[f[t]][1] += dp[t][0];
        
        d[f[t]] --;
        
        if(d[f[t]] == 0)q.push(f[t]);
    }
}


int main()
{
    cin >> n;
    for(int i = 1; i <= n; i ++) scanf("%d", happy + i);
    
    for(int i = 1; i < n; i ++)
    {
        int a, b; scanf("%d %d", &a, &b);
        d[b] ++,f[a] = b;
    }
    
    tupo();
    
    cout << max(dp[root][1] + happy[root], dp[root][0]);
    
    
    
    
    
    return 0;
}
发布了53 篇原创文章 · 获赞 14 · 访问量 1909

猜你喜欢

转载自blog.csdn.net/weixin_45630535/article/details/104686216