A9364 | Choosing Subtree is Fun
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
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.
输入格式
There are two integers in the first line, $n$ and $k$ ( $1<=k<=n<=10^{5}$ ). 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.
It is guaranteed that the input represents a tree.
输出格式
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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted