CF372D.Choosing Subtree is Fun

省选/NOI-

通过率:0%

时间限制:5.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There is a tree consisting of n vertices. The vertices are numbered from 1 to n.

Let's define the length of an interval [l, r] as the value r - l + 1. The score of a subtree of this tree is the maximum length of such an interval [l, r] that, the vertices with numbers l, l + 1, ..., r belong to the subtree.

Considering all subtrees of the tree whose size is at most k, return the maximum score of the subtree. Note, that in this problem tree is not rooted, so a subtree — is an arbitrary connected subgraph of the tree.

给定一棵由 nn 个顶点构成的树,顶点编号为 11 到 nn。

定义区间 [l, r][l,\,r] 的长度为 r−l+1r - l + 1。该树的一个子树的得分是指满足如下条件的最大区间 [l, r][l,\,r] 的长度:顶点编号 l, l+1, …, rl,\,l+1,\,\dots,\,r 均属于该子树。

考虑该树中所有大小(即所含顶点数)不超过 kk 的子树,求其中得分的最大值。注意:本题中树是无根树,因此“子树”指树的任意一个连通子图。

输入格式

There are two integers in the first line, n and k (1 ≤ k ≤ n ≤ 105). Each of the next n - 1 lines contains integers a__i and b__i (1 ≤ a__i, b__i ≤ n, a__i ≠ b__i). That means a__i and b__i are connected by a tree edge.

It is guaranteed that the input represents a tree.

第一行包含两个整数 nn 和 kk(1 ≤ k ≤ n ≤ 1051 ≤ k ≤ n ≤ 10^5)。接下来的 n − 1n - 1 行每行包含两个整数 aia_i 和 bib_i(1 ≤ ai, bi ≤ n1 ≤ a_i, b_i ≤ n,且 ai ≠ bia_i ≠ b_i),表示 aia_i 与 bib_i 之间存在一条树边。

保证输入数据构成一棵树。

输出格式

Output should contain a single integer — the maximum possible score.

输出应为一个整数——即可能获得的最高分数。

输入输出样例

  • 输入#1

    10 6
    4 10
    10 6
    2 9
    9 6
    8 5
    7 1
    4 7
    7 3
    1 8

    输出#1

    3
  • 输入#2

    16 7
    13 11
    12 11
    2 14
    8 6
    9 15
    16 11
    5 14
    6 15
    4 3
    11 15
    15 14
    10 1
    3 14
    14 7
    1 7

    输出#2

    6

说明/提示

For the first case, there is some subtree whose size is at most 6, including 3 consecutive numbers of vertices. For example, the subtree that consists of {1, 3, 4, 5, 7, 8} or of {1, 4, 6, 7, 8, 10} includes 3 consecutive numbers of vertices. But there is no subtree whose size is at most 6, which includes 4 or more consecutive numbers of vertices.

对于第一种情况,存在某个子树,其大小至多为 6,且包含 3 个编号连续的顶点。例如,由 {1, 3, 4, 5, 7, 8} 或 {1, 4, 6, 7, 8, 10} 构成的子树均包含 3 个编号连续的顶点。但不存在大小至多为 6 的子树,其包含 4 个或更多编号连续的顶点。

输入解题思路,AI测评打分。不知道怎么写?

首页