#P6499. Coin Game on a Tree
Coin Game on a Tree
Coin Game on a Tree
Daniel and Stjepan play a game on a tree with nodes numbered . Initially, a coin is placed at node . The game proceeds in rounds as follows:
- Daniel marks a node (the choice is made by a predetermined strategy).
- Stjepan marks the node where the coin currently is.
- Stjepan then moves the coin to an adjacent node which is unmarked.
The process repeats until Stjepan cannot move the coin, i.e. when the current coin node has no unmarked neighbor. Daniel wants to know if there exists a predetermined marking strategy so that, no matter how Stjepan moves the coin, the total number of coin moves is less than .
A key observation is that since the graph is a tree the coin’s path will be simple. If we fix an ordering (or ranking) of all nodes (except node ) corresponding to the rounds in which Daniel will mark them, then the coin will be forced to stop as soon as it reaches the first node (on its unique –to–v path) that appears in the ordering. In other words, if the first marked node encountered along the coin–path appears in round , then the coin would have moved exactly times. Daniel’s aim is to choose the ordering so that for every path starting at , the first marked node appears among the first positions (so that the coin moves at most times).
It turns out that one may compute an optimal guaranteed (worst–case) coin–move count by rooting the tree at node and defining for every node the value recursively as follows:
- If is a leaf then .
- Otherwise, let for each child (i.e. neighbor of other than its parent) be computed, and sort these values in non–increasing order. Then [ f(v) = \max_{0\le i<d} \Bigl{ i + f(u_i) \Bigr}, ] where is the number of children of and is the child with the –th largest (with starting from ).
Daniel is guaranteed a winning strategy if and only if .
Given , , and the tree structure, determine whether Daniel can guarantee that the coin will move less than times, regardless of Stjepan’s moves.
Note: In the input the tree is given as edges. It is guaranteed that the given graph is a tree.
inputFormat
The first line contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $1 \le k \le n$), representing the number of nodes and the maximum allowed coin moves plus one, respectively.
Each of the next $n-1$ lines contains two integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$) representing an edge between nodes $u$ and $v$.
The tree is guaranteed to be connected.
outputFormat
Output a single line containing either YES
if Daniel has a winning strategy (i.e. he can guarantee that the coin moves fewer than $k$ times), or NO
otherwise.
sample
2 1
1 2
YES