#P11693. [JRKSJ ExR] 构造字符串使得
[JRKSJ ExR] 构造字符串使得
Problem Description
You are given an undirected graph with vertices and edges. A game piece initially is at vertex . This is a two-player game: the first player and the second player take turns moving the piece. Each move, the player may move the piece to any vertex that is directly connected by an edge to the vertex where the piece is currently located. It is guaranteed that every vertex is connected to at least one edge.
There is a variable with initial value . After each move, let be the index of the vertex where the piece is currently located, and then set to . In other words, is the maximum vertex index that the piece has moved to, and the initial position is not counted at the beginning.
The two players will move the piece a total of times. The first player wants the final value of to be as large as possible, while the second player wants the final value of to be as small as possible.
There are queries. Each query gives and asks: if a game of exactly moves starts from , and both players use optimal strategies, what will the final value of be?
Input Format
The first line contains three integers .
The next lines each contain two integers describing an edge in the graph.
The next lines each contain two integers .
Output Format
Output lines. Each line contains one integer, the final value of in that game.
5 5 5
1 2
1 4
2 3
2 5
3 4
1 2
4 1
5 3
1 5
2 2
4
3
4
4
5
Hint
Sample Explanation

The graph in the sample is shown above.
For the first query, clearly the first player cannot force the second player to move to vertex , so the answer is . The first player can achieve this by moving to vertex on the first move.
For the second query, the largest-index vertex that the first player can reach in one move is , so the answer is .
Constraints
This problem uses bundled testdata.
| Special Property | Score | |||
|---|---|---|---|---|
Special property: the graph is guaranteed to be a single chain (a path).
For all testdata, it is guaranteed that , , , and .
It is guaranteed that the given graph has no multiple edges and no self-loops. It is also guaranteed that for any vertex , there exists at least one vertex such that there is an edge between and .
Translated by ChatGPT 5