P28983 [洛谷] [SNOI2019] Network
省选
题目描述

Problem Description

There are $n$ data centers, numbered $1, 2, \ldots, n$. They are connected by $n-1$ optical cables, forming a tree structure.

Each optical cable has a delay of $1$ unit of time, and the delay between two data centers is the sum of the delays of the optical cables connecting them.

We need to select some of these $n$ data centers as communication stations, with the requirement that the delay between any two communication stations does not exceed $d$. Let the selected communication stations be ${w_1, w_2, \ldots, w_k}$, then the total communication delay is the sum of the delays between each pair of these $k$ communication stations.

There are $q$ queries, each specifying a data center $u$. You need to determine: if $u$ is a communication station, what is the maximum possible total communication delay.

Input Format

The first line contains two natural numbers $n$ and $d$, representing the number of data centers and the maximum allowed delay between two communication stations, respectively.

The next $n-1$ lines each contain two positive integers $u$ and $v$, indicating that there is an optical cable between $u$ and $v$.

The next line contains a positive integer $q$, representing the number of queries.

The next $q$ lines each contain a positive integer $u$, representing the specified communication station in the query.

Output Format

Output $q$ lines, each containing an integer representing the answer to the corresponding query.

Sample Input and Output

Input Sample #1

6 2
1 2
2 3
1 4
4 5
4 6
6
1
2
3
4
5
6

Output Sample #1

9
4
4
9
9
9

Input Sample #2

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

Output Sample #2

16
16
4
16
9
9
16
16
9
9

Notes/Hints

For all data, $1 \leq n \leq 5 \times 10^5, 0 \leq d < n, 0 \leq q \leq 10$.

For 10% of the data, $n \leq 15$;

For another 10% of the data, $d = n-1$;

For another 15% of the data, $n \leq 300$;

For another 15% of the data, $n \leq 5000$;

For another 20% of the data, $n \leq 10^5$;

For the remaining 30% of the data, there are no special restrictions.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
6 2 1 2 2 3 1 4 4 5 4 6 6 1 2 3 4 5 6
样例输出
9 4 4 9 9 9