CF1675F.Vlad and Unfinished Business
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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 ai-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 a1,a2,…,ak he can visit in any order. He can visit any house multiple times (if he wants).
弗拉德和娜斯佳住在一座由 n 座房屋和 n−1 条道路组成的城市中。任意两座房屋之间均可仅通过道路相互到达。也就是说,这座城市构成一棵树。
弗拉德住在编号为 x 的房屋中,娜斯佳住在编号为 y 的房屋中。弗拉德决定去拜访娜斯佳。但他想起自己此前推迟了 k 件必须在拜访娜斯佳之前完成的事情。第 i 件事要求他前往编号为 ai 的房屋;这些事情可以按任意顺序完成。每分钟,他可以从一座房屋步行至另一座与其直接由道路相连的房屋。
弗拉德并不喜欢走路,因此他想知道:完成所有事情后最终抵达娜斯佳所在的房屋,他最少需要花费多少分钟在路上?房屋 a1,a2,…,ak 可以按任意顺序访问,且同一座房屋可被多次访问(如需要)。
输入格式
The first line of input contains an integer t (1≤t≤104) — 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≤k≤n≤2⋅105) — the number of houses and things, respectively.
The second line of each test case contains two integers x and y (1≤x,y≤n) — indices of the houses where Vlad and Nastya live, respectively.
The third line of each test case contains k integers a1,a2,…,ak (1≤ai≤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 vj and uj (1≤uj,vj≤n) — indices of houses connected by road j.
It is guaranteed that the sum of n for all cases does not exceed 2⋅105.
输入的第一行包含一个整数 t(1≤t≤104)—— 表示测试用例的数量。每个测试用例前均有一空行。
每个测试用例的第一行包含两个整数 n 和 k(1≤k≤n≤2⋅105)—— 分别表示房屋数量和待办事项数量。
每个测试用例的第二行包含两个整数 x 和 y(1≤x,y≤n)—— 分别表示弗拉德与娜斯佳所居住的房屋编号。
每个测试用例的第三行包含 k 个整数 a1,a2,…,ak(1≤ai≤n)—— 表示弗拉德需前往完成事项的房屋编号。
接下来的 n−1 行描述城市结构,每行包含两个整数 vj 和 uj(1≤uj,vj≤n)—— 表示由第 j 条道路连接的两座房屋编号。
保证所有测试用例的 n 值之和不超过 2⋅105。
输出格式
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.
输出 t 行,每行包含对应输入测试用例的答案。答案为一个整数——即弗拉德完成所有事情并到达纳斯塔亚所需的最少分钟数。
输入输出样例
输入#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→2→1→3
Tree and best path for the second test case:
3→1→3→5→2→5→6→5
Tree and best path for the third test case:
3→5→2
第一个测试用例的树与最优路径:
1→2→1→3
第二个测试用例的树与最优路径:
3→1→3→5→2→5→6→5
第三个测试用例的树与最优路径:
3→5→2
输入解题思路,AI测评打分。不知道怎么写?