Problem A. Hide-And-Seek Game
该比赛已结束,您无法在比赛模式下递交该题目。您可以点击“在题库中打开”以普通模式查看和递交本题。
Description
During the summer vacation, Serenade and Rhapsody are playing hide-and-seek in a park structured as a tree. Each edge of the tree has a weight of 1. Serenade keeps running back and forth between Sa and Ta (Sa 6= Ta), while Rhapsody runs back and forth between Sb and Tb (Sb 6= Tb). However, Aria doesn’t want to run around with them and only wants to know the earliest location where Serenade and Rhapsody will meet. Please output the identification number of this location.If they will never meet, output -1. To be more specific, Serenade starts from Sa and moves one edge towards Ta each time. Once reaching Ta, Serenade then moves one edge towards Sa each time. After reaching Sa, Serenade moves one edge towards Ta each time, and so on. Rhapsody follows a similar pattern of movement. Note that this park is quite mysterious, so Serenade and Rhapsody will not meet on an edge (you can assume that they will choose different paths to traverse the same edge).
Format
Input
The input consists of multiple test cases. The first line contains a single integer t(1 ≤ t ≤ 500) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers n and m (2 ≤ n, m ≤ 3 · 103 ) — the number of the vertices in the given tree and the number of questions. Each of the next n − 1 lines contains two integers u and v (1 ≤ u, v ≤ n, u 6= v) meaning that there is an edge between vertices u and v in the tree. Each of the next m lines contains four integers Sa , Ta , Sb and Tb (1 ≤ Sa, Ta, Sb, Tb ≤ n, Sa 6= Ta and Sb 6= Tb) . It is guaranteed that the given graph is a tree. The data guarantees that there will be no more than 20 groups with a value of n exceeding 400. The data guarantees that there will be no more than 20 groups with a value of m exceeding 400.
Output
For each test case print a single integer — the identification number of this location which Serenade and Rhapsody will meet or -1
Samples
1
9 4
1 2
1 9
2 3
2 6
3 4
3 5
6 7
6 8
4 7 5 8
4 7 2 8
4 5 3 6
4 5 5 7
3
6
-1
3
Limitation
5s, 128 mb for each test case.
杭电“钉耙编程”挑战(纯英纯硬,持续更新ing)
- 状态
- 已结束
- 规则
- ACM/ICPC
- 题目
- 7
- 开始于
- 2023-8-5 12:00
- 结束于
- 2023-9-24 12:00
- 持续时间
- 1200 小时
- 主持人
- 参赛人数
- 28