CF-2277C•Codeforces

Edge Addeleting

dfs and similargreedytrees

Frums - Credits

You are given a tree with nn vertices, numbered from 11 to nn.

You need to perform mm operations on this tree. The ii-th operation consists of two steps:

  1. an edge between vertex 11 and vertex viv_i is added, which creates a cycle;
  2. you choose one edge of this cycle (possibly the newly added one) and delete it, so the graph becomes a tree again.

If there is already an edge between vertex 11 and vertex viv_i, the new edge is still added, and the cycle consists of these two parallel edges.

Find the maximum possible distance∗^{\text{∗}} between vertices 11 and nn after all mm operations.

∗^{\text{∗}}The distance between two vertices in a tree is the number of edges on the unique simple path between them.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains two integers nn and mm (2≤n≤5⋅1052 \le n \le 5 \cdot 10^5, 1≤m≤5⋅1051 \le m \le 5 \cdot 10^5) — the number of vertices and the number of operations.

Each of the next n−1n - 1 lines contains two integers xx and yy (1≤x,y≤n1 \le x, y \le n, x≠yx \ne y) — an edge of the initial tree. It is guaranteed that the given edges form a tree.

The next line contains mm integers v1,v2,…,vmv_1, v_2, \ldots, v_m (2≤vi≤n2 \le v_i \le n), describing the operations.

It is guaranteed that the sum of nn over all test cases does not exceed 5⋅1055 \cdot 10^5.

It is guaranteed that the sum of mm over all test cases does not exceed 5⋅1055 \cdot 10^5.

Output

For each test case, output a single integer — the maximum possible distance between vertices 11 and nn after all mm operations.

Note

In the first test case, the tree consists of a single edge (1,2)(1, 2), and it stays the same after every operation. Thus, the answer is 11.

In the second test case, the distance between vertices 11 and 33 is initially 22, and neither operation can increase it. Thus, the answer is 22.

In the third test case, we can delete the newly added edge in the first operation. In the second operation, the edge (1,5)(1, 5) is added, and we delete the edge (1,2)(1, 2). After that, the path from 11 to 77 is 1→5→4→2→3→71 \to 5 \to 4 \to 2 \to 3 \to 7, so the distance is 55. It can be shown that no larger distance is possible. Thus, the answer is 55.

Sample 1
Input
5
2 2
1 2
2 2
3 2
1 2
2 3
2 3
7 2
1 2
2 3
3 7
2 4
4 5
1 6
6 5
10 4
1 2
2 3
3 10
2 4
4 5
5 6
1 7
7 8
8 9
9 8 6 10
7 1
1 2
2 7
1 3
3 4
4 5
5 6
6
Output
1
2
5
6
2

Discussions & Comments

Share your approach or ask questions about this problem (0)

You must be logged in to comment or start a discussion on this problem.

Log In

No comments yet. Be the first to comment!

Actions

Open on Codeforces

About Platform

CO
Codeforces
External Judge

Problem Discussions

0

Have questions or want to share your solution approach? Join the discussion.

View / write comments