codevs1163访问艺术馆 树形dp

算裸的树形dp吧 回来复习一波

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int M=1007;
int read(){
    int ans=0,f=1,c=getchar();
    while(c<'0'||c>'9'){if(c=='-') f=-1; c=getchar();}
    while(c>='0'&&c<='9'){ans=ans*10+(c-'0'); c=getchar();}
    return ans*f;
}
int tot,cnt=1,k;
int f[M][1005];
struct node{int w,v,l,r;}tr[M];
void dfs(int x){
    tr[x].v=read(); k=read();
    cnt++;
    if(!k){
        tr[x].l=cnt; dfs(tr[x].l);
        tr[x].r=cnt; dfs(tr[x].r);
    }else tr[x].w=k;
}
void trdp(int x){
    if(!tr[x].l&&!tr[x].r){
        for(int i=2*tr[x].v;i<=tot;i++){
            int now=(i-2*tr[x].v)/5;
            if(now<=tr[x].w) f[x][i]=now;
            else f[x][i]=tr[x].w;
        }
        return ;
    }
    if(tr[x].l) trdp(tr[x].l);
    if(tr[x].r) trdp(tr[x].r);
    for(int i=2*tr[x].v;i<=tot;i++){
        int l=tr[x].l,r=tr[x].r;
        for(int j=0;j<=i-(2*tr[x].v);j++) 
            f[x][i]=max(f[x][i],f[l][j]+f[r][i-(2*tr[x].v)-j]);
    }
}
int main()
{
    tot=read(); 
    dfs(1); trdp(1);
    printf("%d\n",f[1][tot]);
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/lyzuikeai/p/7086838.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值