Preparando MOJI

Game On Tree

3000ms 524288K

Description:

Alice and Bob are playing a game. They have a tree consisting of $$$n$$$ vertices. Initially, Bob has $$$k$$$ chips, the $$$i$$$-th chip is located in the vertex $$$a_i$$$ (all these vertices are unique). Before the game starts, Alice will place a chip into one of the vertices of the tree.

The game consists of turns. Each turn, the following events happen (sequentially, exactly in the following order):

  1. Alice either moves her chip to an adjacent vertex or doesn't move it;
  2. for each Bob's chip, he either moves it to an adjacent vertex or doesn't move it. Note that this choice is done independently for each chip.

The game ends when Alice's chip shares the same vertex with one (or multiple) of Bob's chips. Note that Bob's chips may share the same vertex, even though they are in different vertices at the beginning of the game.

Alice wants to maximize the number of turns, Bob wants to minimize it. If the game ends in the middle of some turn (Alice moves her chip to a vertex that contains one or multiple Bob's chips), this turn is counted.

For each vertex, calculate the number of turns the game will last if Alice places her chip in that vertex.

Input:

The first line contains one integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the number of vertices in the tree.

Then $$$n - 1$$$ lines follow, each line contains two integers $$$u_i$$$, $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$; $$$u_i \ne v_i$$$) that denote the endpoints of an edge. These edges form a tree.

The next line contains one integer $$$k$$$ ($$$1 \le k \le n - 1$$$) — the number of Bob's chips.

The last line contains $$$k$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_k$$$ ($$$1 \le a_i \le n$$$; $$$a_i \ne a_j$$$ if $$$i \ne j$$$) — the vertices where the Bob's chips are initially placed.

Output:

Print $$$n$$$ integers. The $$$i$$$-th of them should be equal to the number of turns the game will last if Alice initially places her chip in the vertex $$$i$$$. If one of Bob's chips is already placed in vertex $$$i$$$, then the answer for vertex $$$i$$$ is $$$0$$$.

Sample Input:

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

Sample Output:

2 1 2 0 0

Sample Input:

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

Sample Output:

3 0 0 3 1 2 3 0

Sample Input:

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

Sample Output:

0 2 2 2 2 0 2 2 0 0

Informação

Codeforces

Provedor Codeforces

Código CF1452G

Tags

data structuresdfs and similargreedytrees

Submetido 0

BOUA! 0

Taxa de BOUA's 0%

Datas 09/05/2023 10:08:53

Relacionados

Nada ainda