#P10301. [THUWC 2020] Yazid 的棋
[THUWC 2020] Yazid 的棋
Problem Description
Given a directed graph with vertices and edges, the vertices are numbered from to , and the edges are numbered from to .
The owner of this graph, Yazid, will perform operations in order. Each operation is as follows: place a chess piece on a vertex and set an integer . Meanwhile, the piece keeps moving along the outgoing edge with the smallest index. When the -th edge has been traversed times, it will be deleted from the graph immediately. The piece keeps moving until there is no outgoing edge to take or the piece has moved steps. The vertex where the piece finally stops is called the destination of this operation. After that, Yazid removes the piece from the game.
You need to predict the destination vertex index for each operation.
Note that all operations have aftereffects. That is, the deleted edges and the traversal counts that happened in the first operations will not be reset in the -th operation. Each operation is performed based on the state left by the previous operations.
Input Format
The first line contains two integers separated by spaces.
The next lines describe the directed graph. In this part, the -th line () contains three integers separated by spaces, describing a directed edge from to that will be deleted after being traversed times.
- It is guaranteed that , and .
The next line contains an integer .
The next lines describe the operations in order. Each line contains two integers separated by spaces, describing an operation.
- It is guaranteed that , .
Output Format
Output lines, each containing one integer, printing the destination of each operation in order. The -th line should be the destination of the -th operation.
7 8
3 1 3
1 2 5
2 3 2
6 3 1
5 2 2
4 2 1
7 5 3
2 2 2
3
7 7
6 2
6 2
1
1
6
见附件中的 2.in。
见附件中的 2.ans。
Hint
[Sample Explanation #1]
In the first operation, the piece moves as follows:
- Place a piece on vertex .
- The outgoing edge of vertex with the smallest index is edge , so the piece moves to vertex through this edge.
- The outgoing edge of vertex with the smallest index is edge , so the piece moves to vertex through this edge.
- The outgoing edge of vertex with the smallest index is edge , so the piece moves to vertex through this edge.
- The outgoing edge of vertex with the smallest index is edge , so the piece moves to vertex through this edge.
- The outgoing edge of vertex with the smallest index is edge , so the piece moves to vertex through this edge.
- The outgoing edge of vertex with the smallest index is edge , so the piece moves to vertex through this edge. At this time, edge has been traversed times and is deleted from the graph.
- The outgoing edge of vertex with the smallest index is edge , so the piece moves to vertex through this edge.
- The piece stops at vertex , so the destination of this operation is vertex .
In the second operation, the piece moves as follows:
- Place a piece on vertex .
- The outgoing edge of vertex with the smallest index is edge , so the piece moves to vertex through this edge. At this time, edge has been traversed time and is deleted from the graph.
- The outgoing edge of vertex with the smallest index is edge , so the piece moves to vertex through this edge. At this time, edge has been traversed time and is deleted from the graph.
- The piece has already moved steps, so it stops at vertex . Vertex is the destination of this operation.
In the third operation, the piece moves as follows:
- Place a piece on vertex .
- Vertex has no outgoing edges, so the piece can only stop at vertex . Therefore, the destination of this operation is vertex .
[Subtasks]
This problem has multiple subtasks. Each subtask may contain multiple test points. You can get the score of a subtask only if you pass all test points in that subtask. The test points of each subtask satisfy some special constraints, as shown in the table below.
| Subtask ID | Property | Score | |||||
|---|---|---|---|---|---|---|---|
| 1 | 8 | ||||||
| 2 | |||||||
| 3 | |||||||
| 4 | |||||||
| 5 | 13 | ||||||
| 6 | 15 | ||||||
| 7 | 8 | ||||||
| 8 | 28 |
For all test points, it is guaranteed that , , , , .
- Property 0: no special property.
- Property 1: the graph is generated randomly, and each edge appears with probability .
- Property 2: it is guaranteed that .
- Property 3: it is guaranteed that the graph forms a rooted tree.
- Property 4: it is guaranteed that the graph forms a “cycle-with-trees” (huan tao shu).
- Property 5: it is guaranteed that the first edges form a “cycle-with-trees” (huan tao shu).
Translated by ChatGPT 5