当前你的浏览器版本过低,网站已在兼容模式下运行,兼容模式仅提供最小功能支持,网站样式可能显示不正常。
请尽快升级浏览器以体验网站在线编辑、在线运行等功能。

建议使用的浏览器:

谷歌Chrome 火狐Firefox Opera浏览器 微软Edge浏览器 QQ浏览器 360浏览器 傲游浏览器

6031:Innumerable Ancestors

题目描述
There is a tree having $n$ nodes, labeled from 1 to $n$. The root of the tree is always 1, and the depth of a node $p$ is the number of nodes on the shortest path between node $p$ and the root.
In computer science, the Lowest Common Ancestor (LCA) of two nodes $v$ and $w$ in a tree is the lowest (i.e. deepest) node that has both $v$ and $w$ as descendants, where we define each node to be a descendant of itself (so if $v$ has a direct connection from $w$, $w$ is the lowest common ancestor).
You have to answer $m$ queries. Each query gives two non-empty node sets $A$ and $B$, there might be some nodes in both sets.
You should select one node $x$ from set $A$, and one node $y$ from set $B$, $x$ and $y$ can be the same node. Your goal is to maximize the depth of the LCA of $x$ and $y$.
Please write a program to answer these queries.
输入解释
The input contains several test cases, no more than 5 test cases.
In each test case, the first line contains two integers $n(1\leq n\leq 100000)$ and $m(1\leq m\leq 100000)$, denoting the number of nodes and queries.
For the next $n-1$ lines,each line contians two integers $a$ and $b$, denoting a bi-directional edge between node $a$ and $b$.
Then there are $2m$ lines, every two lines describes one query.
For each query, the first line describes the set A.
The first integer $k(1\leq k\leq n)$ denotes the number of nodes in set $A$, and the next $k$ integers describing the nodes in set $A$. There might be some nodes appear multiple times in the set.
The second line describes the set $B$ in the same format of set $A$.

It is guaranteed that $\sum k\leq 100000$ in each test case.
输出解释
For every query, print a number denoting the answer, which means the maximum depth of the LCA.
输入样例
7 3
1 2
1 3
3 4
3 5
4 6
4 7
1 6
1 7
2 6 7
1 7
2 5 4
2 3 2
输出样例
3
4
2
来自杭电HDUOJ的附加信息
Recommend

该题目是Virtual Judge题目,来自 杭电HDUOJ

源链接: HDU-6031

最后修改于 2020-10-25T23:27:42+00:00 由爬虫自动更新

共提交 0

通过率 --%
时间上限 内存上限
10000/5000MS(Java/Others) 131072/131072K(Java/Others)