lalalala


私信TA

用户名:zhangshuo

访问量:152019

签 名:

像狗一样的学习,像绅士一样地玩耍。

等  级
排  名 6
经  验 30160
参赛次数 10
文章发表 201
年  龄 12
在职情况 学生
学  校 芜湖市第十一中学
专  业

  自我简介:

今日懒惰流下的口水,将会成为明日里伤心的泪水。

解题思路:





注意事项:





参考代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=6e3+10;
int n;
int dp[maxn][2];//记录状态 
int hap[maxn];//快乐值 
bool no_head[maxn];//记录是否是校长 true->不是 
vector<int>down[maxn];//记录每个人的下属 
int dfs(int now,bool if_get)
{    
    if(dp[now][if_get]) return dp[now][if_get];    //递归终点 
    if(!down[now].size()) return hap[now]*if_get;    //只有参加,才能贡献快乐值 
    int res=0;    
    if(if_get){        
    for(int i=0;i<down[now].size();i++){            
    int &nxt=down[now][i];
    res+=dfs(nxt,false);
        }
    }    
    else
    {       for(int i=0;i<down[now].size();i++){            
            int &nxt=down[now][i];
            res+=max(dfs(nxt,true),dfs(nxt,false));            //下属参加或不参加的最大值才是最佳贡献 
        }
    }    
        return dp[now][if_get]=if_get?res+hap[now]:res;    //如果这个人参加,不要忘了返回他自身的快乐值 }int main(){    cin>>n;    for(int i=1;i<=n;i++) scanf("%d",&hap[i]);    int a,b;    while(scanf("%d%d",&a,&b)&&a&&b){
        down[b].push_back(a);
        no_head[a]=true;
    }    
        for(int i=1;i<=n;i++) if(!no_head[i])   
        cout<<max(dfs(i,false),dfs(i,true));    
        return 0;
}


 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区