P1352 没有上司的舞会

 

 

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<bits/stdc++.h>
#define ll long long
#define ddd printf("-----------------debug\n");
using namespace std;
const int maxn=6e3+10;

int head[maxn],to[maxn<<1],nxt[maxn<<1],tot;
int f[maxn][10],is_head[maxn],n,ans;

void add(int u,int v){
    to[++tot]=v; nxt[tot]=head[u]; head[u]=tot;
}
void dfs(int u)//dfs-> vis 防止 重复搜索 但这里 树形 之搜索一次 
{
    //vis[u]=1;
    for(int i=head[u];i;i=nxt[i])
    {
        int v=to[i]; //if(vis[v]) continue;
        dfs(v);
        f[u][1]+=f[v][0];
        f[u][0]+=max(f[v][1],f[v][0]);
    }
}
int main()
{
    ios::sync_with_stdio(false); cin.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++) cin>>f[i][1];
    for(int i=1;i<=n-1;i++){
        int l,k; cin>>l>>k;
        is_head[k]=1; is_head[l]=0;
        add(k,l);
    }
    for(int i=1;i<=n;i++){
        if(is_head[i])
        {
            //cout<<i<<endl;
            dfs(i);
            ans=max(f[i][0],f[i][1]); cout<<ans<<'\n';
            return 0;
        }
    }
    return 0;
}
View Code

 

 
posted @ 2023-08-12 22:29  JMXZ  阅读(6)  评论(0)    收藏  举报