#P6584. Little Z and the Youyou Tree Battle
Little Z and the Youyou Tree Battle
Little Z and the Youyou Tree Battle
Little Z and \(m\) Youyous have met on a tree! The tree is undirected and has \(n\) nodes; every edge has length 1. Initially, Little Z stands at node \(x\) and carries a gun that can kill any Youyou within a distance of \(k\) (i.e. if the distance is \(\le k\), the shot is fatal). Meanwhile, Youyous are not very clever – once shot they die immediately – and Little Z himself is invincible.
The game proceeds in rounds. At the beginning of each round, the following events occur in order:
- The round counter increases by 1 (initially 0).
- Little Z shoots and kills every Youyou whose distance from him on the tree is \(\le k\) (using the gun’s range \(k\)).
- If all Youyous are eliminated, the game ends and the current round counter is the number of rounds used.
- Little Z may either move to an adjacent node (along an edge) or stay at his current position.
- Every remaining Youyou moves one step (i.e. along one edge) toward Little Z along the unique simple path. (If a Youyou is already on the same node as Little Z, it does not move.)
Given the structure of the tree and the initial positions of the Youyous, help Little Z determine the minimum number of rounds needed to eliminate all the adversaries if he follows an optimal strategy.
Note: Because Youyous move every round (after Little Z’s movement) along their unique shortest paths and Little Z may only choose one direction to move in per round, his strategy may be to chase the enemies in one branch (thereby effectively reducing the distance by 2 per round on that branch) while waiting for the enemies in other branches to approach. Formally, for an enemy whose initial distance from \(x\) is \(d\), if Little Z never leaves \(x\) the enemy will be shot in round \(d-k+1\) (provided \(d>k\); otherwise it is killed in round 1). However, if Little Z moves optimally along the unique path toward that enemy then the enemy’s effective rounds is \(\lceil\frac{d-k}{2}\rceil+1\). In the tree, the neighbors of \(x\) define different branches. Little Z may choose one branch to chase (thus using the faster rate of approach) while for all the other branches he remains at \(x\) so that the corresponding enemy in that branch is killed in \(d-k+1\) rounds. The answer is the minimum number of rounds required over all such choices.
You are given a tree with \(n\) nodes, \(m\) enemy positions, the starting node \(x\) of Little Z, and the shooting range \(k\).
inputFormat
The first line contains four integers \(n\), \(m\), \(x\) and \(k\) \((1 \le x \le n,\, 1 \le n \le 10^5,\, 0 \le m \le 10^5,\, 1 \le k \le n)\) — the number of nodes, the number of enemies, the starting node of Little Z, and the gun's range.
Each of the next \(n-1\) lines contains two integers \(u\) and \(v\) \((1 \le u,v \le n)\) denoting an edge of the tree.
The last line contains \(m\) integers \(a_1, a_2, \dots, a_m\) \((1 \le a_i \le n)\) representing the initial positions of the Youyous. (If \(m=0\), this line may be omitted.)
outputFormat
Output a single integer: the minimum number of rounds required to eliminate all the Youyous.
sample
10 1 1 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10
5
</p>