A14974 | Vlad and Unfinished Business
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Vlad and Nastya live in a city consisting of $n$ houses and $n-1$ road. From each house, you can get to the other by moving only along the roads. That is, the city is a tree.
Vlad lives in a house with index $x$ , and Nastya lives in a house with index $y$ . Vlad decided to visit Nastya. However, he remembered that he had postponed for later $k$ things that he has to do before coming to Nastya. To do the $i$ -th thing, he needs to come to the $a_i$ -th house, things can be done in any order. In $1$ minute, he can walk from one house to another if they are connected by a road.
Vlad does not really like walking, so he is interested what is the minimum number of minutes he has to spend on the road to do all things and then come to Nastya. Houses $a_1, a_2, \dots, a_k$ he can visit in any order. He can visit any house multiple times (if he wants).
Vlad lives in a house with index $x$ , and Nastya lives in a house with index $y$ . Vlad decided to visit Nastya. However, he remembered that he had postponed for later $k$ things that he has to do before coming to Nastya. To do the $i$ -th thing, he needs to come to the $a_i$ -th house, things can be done in any order. In $1$ minute, he can walk from one house to another if they are connected by a road.
Vlad does not really like walking, so he is interested what is the minimum number of minutes he has to spend on the road to do all things and then come to Nastya. Houses $a_1, a_2, \dots, a_k$ he can visit in any order. He can visit any house multiple times (if he wants).
输入格式
The first line of input contains an integer $t$ ( $1 \le t \le 10^4$ ) — the number of input test cases. There is an empty line before each test case.
The first line of each test case contains two integers $n$ and $k$ ( $1 \le k \le n \le 2\cdot 10^5$ ) — the number of houses and things, respectively.
The second line of each test case contains two integers $x$ and $y$ ( $1 \le x, y \le n$ ) — indices of the houses where Vlad and Nastya live, respectively.
The third line of each test case contains $k$ integers $a_1, a_2, \dots, a_k$ ( $1 \le a_i \le n$ ) — indices of houses Vlad need to come to do things.
The following $n-1$ lines contain description of city, each line contains two integers $v_j$ and $u_j$ ( $1 \le u_j, v_j \le n$ ) — indices of houses connected by road $j$ .
It is guaranteed that the sum of $n$ for all cases does not exceed $2\cdot10^5$ .
The first line of each test case contains two integers $n$ and $k$ ( $1 \le k \le n \le 2\cdot 10^5$ ) — the number of houses and things, respectively.
The second line of each test case contains two integers $x$ and $y$ ( $1 \le x, y \le n$ ) — indices of the houses where Vlad and Nastya live, respectively.
The third line of each test case contains $k$ integers $a_1, a_2, \dots, a_k$ ( $1 \le a_i \le n$ ) — indices of houses Vlad need to come to do things.
The following $n-1$ lines contain description of city, each line contains two integers $v_j$ and $u_j$ ( $1 \le u_j, v_j \le n$ ) — indices of houses connected by road $j$ .
It is guaranteed that the sum of $n$ for all cases does not exceed $2\cdot10^5$ .
输出格式
Output $t$ lines, each of which contains the answer to the corresponding test case of input. As an answer output single integer — the minimum number of minutes Vlad needs on the road to do all the things and come to Nastya.
输入输出样例
输入 #1
3 3 1 1 3 2 1 3 1 2 6 4 3 5 1 6 2 1 1 3 3 4 3 5 5 6 5 2 6 2 3 2 5 3 1 3 3 4 3 5 5 6 5 2
输出 #1
3 7 2
Tree and best path for the first test case:
 $1 \rightarrow 2 \rightarrow 1 \rightarrow 3$ Tree and best path for the second test case:
 $3 \rightarrow 1 \rightarrow 3 \rightarrow 5 \rightarrow 2 \rightarrow 5 \rightarrow 6 \rightarrow 5$ Tree and best path for the third test case:
 $3 \rightarrow 5 \rightarrow 2$
 $1 \rightarrow 2 \rightarrow 1 \rightarrow 3$ Tree and best path for the second test case:
 $3 \rightarrow 1 \rightarrow 3 \rightarrow 5 \rightarrow 2 \rightarrow 5 \rightarrow 6 \rightarrow 5$ Tree and best path for the third test case:
 $3 \rightarrow 5 \rightarrow 2$
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted