codeforces 1065f Up and Down the Tree 树形dp

F. Up and Down the Tree

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a tree with nn vertices; its root is vertex 11. Also there is a token, initially placed in the root. You can move the token to other vertices. Let's assume current vertex of token is vv, then you make any of the following two possible moves:

  • move down to any leaf in subtree of vv;
  • if vertex vv is a leaf, then move up to the parent no more than kk times. In other words, if h(v)h(v) is the depth of vertex vv (the depth of the root is 00), then you can move to vertex toto such that toto is an ancestor of vv and h(v)−k≤h(to)h(v)−k≤h(to).

Consider that root is not a leaf (even if its degree is 11). Calculate the maximum number of different leaves you can visit during one sequence of moves.

Input

The first line contains two integers nn and kk (1≤k<n≤1061≤k<n≤106) — the number of vertices in the tree and the restriction on moving up, respectively.

The second line contains n−1n−1 integers p2,p3,…,pnp2,p3,…,pn, where pipi is the parent of vertex ii.

It is guaranteed that the input represents a valid tree, rooted at 11.

Output

Print one integer — the maximum possible number of different leaves you can visit.

Examples

input

Copy

7 1
1 1 3 3 4 4

output

Copy

4

input

Copy

8 2
1 1 2 3 4 5 5

output

Copy

2

题目大意:有一颗树,假设当前处于节点x,若x不是叶结点,那么可以到达x所在子树的任意一个叶结点上,如果x是叶结点,那么可以到达x的所有距离不超过k的祖先节点上,问当起点为根节点时最多可以访问多少个叶结点。

dp,

dp[u][0]表示在节点u访问u的子树之后必须回来u节点的最大访问叶结点数

dp[u][1]表示在节点u访问u的子树之后停留在u的子树内部的最大访问叶结点数

其中dp[u][0]为所有可以返回到u的子树v的dp[v][0]之和

而dp[u][1] = max(dp[v][1] - dp[v][0]) + sigma(dp[v][0], v为u的儿子节点),即枚举最后停在哪个子树内部。

#include <cstdio>
#include <vector>
using namespace std;
const int N = 1e6 + 10;
vector<int> G[N];
int h[N], dp[N][2], n, k, x;

void dfs(int u, int fa) {
    if (G[u].size() == 0) {
        h[u] = k;
        dp[u][0] = dp[u][1] = 1;
        return;
    }
    h[u] = dp[u][0] = dp[u][1] = 0;
    for (int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        dfs(v, u);
        h[u] = max(h[v] - 1, h[u]);
        if (h[v] > 0) dp[u][0] += dp[v][0];
    }
    for (int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        if (h[v] > 0) dp[u][1] = max(dp[u][1], dp[u][0] - dp[v][0] + dp[v][1]);
        else dp[u][1] = max(dp[u][1], dp[u][0] + dp[v][1]);
    }
}

int main() {
    scanf("%d%d", &n, &k);
    for (int i = 2; i <= n; i++) {
        scanf("%d", &x);
        G[x].push_back(i);
    }
    dfs(1, -1);
    printf("%d\n", dp[1][1]);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值