QOJ.ac

QOJ

Time Limit: 4 s Memory Limit: 512 MB Total points: 100

#359. Network

Statistics

There are $n$ data centers, numbered $1, 2, \dots, n$. They are connected by $n-1$ fiber optic cables, forming a tree.

Each fiber optic cable has a latency of $1$ unit of time. The latency between two data centers is the sum of the latencies of the cables connecting them.

We need to select a subset of these $n$ data centers to serve as communication stations, such that the latency between any two communication stations does not exceed $d$. Let the selected communication stations be $\{w_1, w_2, \dots, w_k\}$. The total communication latency is defined as the sum of the latencies between all pairs of these $k$ communication stations.

There are $q$ queries. For each query, a data center $u$ is selected. You need to find the maximum possible total communication latency if $u$ is required to be one of the communication stations.

Input

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

The next $n-1$ lines each contain two positive integers $u, v$, representing a fiber optic 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 selected communication station for the query.

Output

Output $q$ lines, each containing an integer representing the answer for that query.

Examples

Input 1

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

Output 1

9
4
4
9
9
9

Input 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 2

16
16
4
16
9
9
16
16
9
9

Subtasks

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

  • For $10\%$ of the data, $n \le 15$;
  • For another $10\%$ of the data, $d = n-1$;
  • For another $15\%$ of the data, $n \le 300$;
  • For another $15\%$ of the data, $n \le 5000$;
  • For another $20\%$ of the data, $n \le 10^5$;
  • For the remaining $30\%$ of the data, there are no special restrictions.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.