A12879 | The Maximum Subtree
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Assume that you have $k$ one-dimensional segments $s_1, s_2, \dots s_k$ (each segment is denoted by two integers — its endpoints). Then you can build the following graph on these segments. The graph consists of $k$ vertexes, and there is an edge between the $i$ -th and the $j$ -th vertexes ( $i \neq j$ ) if and only if the segments $s_i$ and $s_j$ intersect (there exists at least one point that belongs to both of them).
For example, if $s_1 = [1, 6], s_2 = [8, 20], s_3 = [4, 10], s_4 = [2, 13], s_5 = [17, 18]$ , then the resulting graph is the following:
A tree of size $m$ is good if it is possible to choose $m$ one-dimensional segments so that the graph built on these segments coincides with this tree.
You are given a tree, you have to find its good subtree with maximum possible size. Recall that a subtree is a connected subgraph of a tree.
Note that you have to answer $q$ independent queries.
For example, if $s_1 = [1, 6], s_2 = [8, 20], s_3 = [4, 10], s_4 = [2, 13], s_5 = [17, 18]$ , then the resulting graph is the following:
A tree of size $m$ is good if it is possible to choose $m$ one-dimensional segments so that the graph built on these segments coincides with this tree.
You are given a tree, you have to find its good subtree with maximum possible size. Recall that a subtree is a connected subgraph of a tree.
Note that you have to answer $q$ independent queries.
输入格式
The first line contains one integer $q$ ( $1 \le q \le 15 \cdot 10^4$ ) — the number of the queries.
The first line of each query contains one integer $n$ ( $2 \le n \le 3 \cdot 10^5$ ) — the number of vertices in the tree.
Each of the next $n - 1$ lines contains two integers $x$ and $y$ ( $1 \le x, y \le n$ ) denoting an edge between vertices $x$ and $y$ . It is guaranteed that the given graph is a tree.
It is guaranteed that the sum of all $n$ does not exceed $3 \cdot 10^5$ .
The first line of each query contains one integer $n$ ( $2 \le n \le 3 \cdot 10^5$ ) — the number of vertices in the tree.
Each of the next $n - 1$ lines contains two integers $x$ and $y$ ( $1 \le x, y \le n$ ) denoting an edge between vertices $x$ and $y$ . It is guaranteed that the given graph is a tree.
It is guaranteed that the sum of all $n$ does not exceed $3 \cdot 10^5$ .
输出格式
For each query print one integer — the maximum size of a good subtree of the given tree.
输入输出样例
输入 #1
1 10 1 2 1 3 1 4 2 5 2 6 3 7 3 8 4 9 4 10
输出 #1
8
In the first query there is a good subtree of size $8$ . The vertices belonging to this subtree are ${9, 4, 10, 2, 5, 1, 6, 3}$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted