Problem 1240 --K people travel on a tree
1240: K people travel on a treeTime Limit: 1 Sec Memory Limit: 128 MB
Submit: 269 Solved: 62
There are N cities numbered from 1 to N and N-1 roads connecting these N cities, or consider it is a tree with N nodes. Each road takes 1 day to travel through. There are K people initially stays at different K cities. They decide to meet at the same city as soon as possible. Please find the minimal time needed.
The first line will be an integer \(T (1 \leq T \leq 10)\), which is the number of test cases.
For each test data:
The first line contains two integers \(N (1 \leq N \leq 10^5)\) and \(K (1 \leq K \leq N)\) — the number of cities and the number of friends.
The next N - 1 lines contain two integers A and B, which means there is a road between city A and city B.
Then the next one line contains K integers, the i-th integer \(p_i\) indicates the place they initially stay.
For each case, contains one line, print the minimal time.