P25408 [AtCoder] Stretching Edges
提高+
题目描述

Problem Description

Takahashi has a stuffed animal in the shape of a tree with $ N $ vertices. The vertices are numbered $ 1, 2, ..., N $.

The $ i $ -th edge connects vertices $ a_i $ and $ b_i $ and has a length of $ 1 $.

We define ${\rm\ dist}(u, v)$ as the shortest distance from vertex $ u $ to vertex $ v $. The diameter of the tree is then ${\rm\ max}_{1 ≦ u < v ≦ N}({\rm\ dist}(u, v))$.

Aoki played a prank on this stuffed animal by selecting an edge and increasing its length by $ 1 $ several times. It is known that the number of pranks is one of $ K_1, K_2, ..., K_Q $.

Aoki likes trees with a shorter diameter, so it is known that he performed the pranks in a way that minimizes the diameter of the tree after all pranks.

For each case where the number of pranks is $ K_1, K_2, ..., K_Q $, find the diameter of the tree after all pranks.

Input Format

The input is given from the standard input in the following format:

$ N $ $ a_1 $ $ b_1 $ $ a_2 $ $ b_2 $ $ : $ $ a_{N-1} $ $ b_{N-1} $ $ Q $ $ K_1 $ $ K_2 $ ... $ K_Q $

Output Format

Output $ Q $ lines. The $ i $ -th line should contain the diameter of the tree when the number of pranks is $ K_i $.

Sample Input and Output

Sample Input #1

4
1 2
1 3
1 4
10
0 1 2 3 4 5 6 7 8 9

Sample Output #1

2
3
4
4
5
6
6
7
8
8

Sample Input #2

9
1 4
2 4
3 4
4 5
5 6
6 7
7 8
8 9
10
0 1 2 3 4 5 6 7 8 9

Sample Output #2

6
7
7
7
8
8
8
9
9
9

Sample Input #3

6
6 3
3 4
3 2
3 1
1 5
31
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

Sample Output #3

3
4
4
4
5
6
6
6
7
8
8
8
9
10
10
10
11
12
12
12
13
14
14
14
15
16
16
16
17
18
18

Notes/Hints

Constraints

  • $ 3 ≦ N ≦ 200,000 $
  • $ 1 ≦ a_i, b_i ≦ N $
  • The input forms a tree
  • $ 1 ≦ Q ≦ 200,000 $
  • $ 0 ≦ K_1 < K_2 < ... < K_Q ≦ 10^{18} $
输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
4 1 2 1 3 1 4 10 0 1 2 3 4 5 6 7 8 9
样例输出
2 3 4 4 5 6 6 7 8 8