Codeforces 1065F(树形dp)

题目链接

题意

给一棵树,进行如下操作,如果当前点非叶子,则往子树移动,否则最多向上移动k次,问从根节点开始最多访问多少叶子

思路

预处理出每个点最多能“白嫖”到几个叶子,根据下一个点的状态更新最优方案

代码

#include <bits/stdc++.h>
#define DBG(x) cerr << #x << " = " << x << endl;
const int maxn = 1e6+5;
const int maxm = 2e6+5;
using namespace std;

int n,k;
int tot,head[maxn];
int a[maxn],b[maxn];
int ans;

struct edgenode{
    int to,next;
}edge[maxm];

void addedge(int u,int v){
    edge[tot].to=v;
    edge[tot].next=head[u];
    head[u]=tot++;
}

void dfs1(int x){
    if(head[x] == -1){
        a[x]=1;
        b[x]=k;
        return;
    }
    for(int i=head[x];i != -1;i=edge[i].next){
        int v=edge[i].to;
        dfs1(v);
        if(b[v])a[x]+=a[v];
        b[x]=max(b[x],b[v]-1);
    }
}

void dfs2(int x,int val){
    if(head[x] == -1){
        ans=max(ans,val+1);
        return;
    }
    for(int i=head[x];i != -1;i=edge[i].next){
        int v=edge[i].to;
        if(b[v])dfs2(v,val+a[x]-a[v]);
        else dfs2(v,val+a[x]);
    }
}

int main(){
    memset(head,-1,sizeof head);
    scanf("%d%d",&n,&k);
    for(int i=2;i<=n;i++){
        int x;
        scanf("%d",&x);
        addedge(x,i);
    }
    dfs1(1);
    dfs2(1,0);
    printf("%d\n",ans);
    return 0;
}

 

转载于:https://www.cnblogs.com/DuskOB/p/10034536.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值