#P11516. Optimal Travel Game on a City Chain

    ID: 13603 Type: Default 1000ms 256MiB

Optimal Travel Game on a City Chain

Optimal Travel Game on a City Chain

There are \(N\) cities numbered from \(1\) to \(N\), and \(N-1\) roads connecting them such that the graph forms a tree. For the purpose of this problem, the tree is guaranteed to be a chain (i.e. a simple path). Specifically, the \(i\)th road connects cities \(u_i\) and \(v_i\) for \(1 \le i \le N-1\).

Alice and Bob are playing a game starting from city \(R\). They take turns driving, with Alice moving first. On Alice's turn, she must drive along exactly \(A\) roads that have not been traversed before. On Bob's turn, he may drive along at most \(B\) roads (possibly zero) that have not been used before.

If at the start of Alice's turn she is unable to travel exactly \(A\) unused roads from her current city, then Bob will immediately take control and drive along up to \(B\) unused roads. Both players are assumed to play optimally: Alice wants the final city's number to be as large as possible, and Bob wants it to be as small as possible.

Determine the final city number where the game ends under optimal play.

inputFormat

The first line contains four integers \(N\), \(A\), \(B\), and \(R\): the number of cities, the number of roads Alice must travel on her turn, the maximum number of roads Bob can travel on his turn, and the starting city respectively.

The following \(N-1\) lines each contain two integers \(u\) and \(v\), indicating that there is an undirected road connecting cities \(u\) and \(v\). It is guaranteed that the given graph is a tree and for the test cases provided, the tree is a chain (i.e. a single straight path).

outputFormat

Output a single integer representing the final city number where the game ends, assuming both players play optimally.

sample

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