#P10301. [THUWC 2020] Yazid 的棋

[THUWC 2020] Yazid 的棋

Problem Description

Given a directed graph with nn vertices and mm edges, the vertices are numbered from 11 to nn, and the edges are numbered from 11 to mm.

The owner of this graph, Yazid, will perform QQ operations in order. Each operation is as follows: place a chess piece on a vertex xx and set an integer ss. Meanwhile, the piece keeps moving along the outgoing edge with the smallest index. When the ii-th edge has been traversed wiw_i 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 ss 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 Q1Q-1 operations will not be reset in the QQ-th operation. Each operation is performed based on the state left by the previous operations.

Input Format

The first line contains two integers n,mn,m separated by spaces.

The next mm lines describe the directed graph. In this part, the ii-th line (1im1\leq i\leq m) contains three integers ui,vi,wiu_i,v_i,w_i separated by spaces, describing a directed edge from uiu_i to viv_i that will be deleted after being traversed wiw_i times.

  • It is guaranteed that 1ui,vin1\leq u_i,v_i\leq n, and wi1018w_i\leq 10^{18}.

The next line contains an integer QQ.

The next QQ lines describe the operations in order. Each line contains two integers x,sx,s separated by spaces, describing an operation.

  • It is guaranteed that 1xn1\leq x\leq n, 1s1091\leq s\leq 10^{9}.

Output Format

Output QQ lines, each containing one integer, printing the destination of each operation in order. The ii-th line should be the destination of the ii-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:

  1. Place a piece on vertex 77.
  2. The outgoing edge of vertex 77 with the smallest index is edge 77, so the piece moves to vertex 55 through this edge.
  3. The outgoing edge of vertex 55 with the smallest index is edge 55, so the piece moves to vertex 22 through this edge.
  4. The outgoing edge of vertex 22 with the smallest index is edge 33, so the piece moves to vertex 33 through this edge.
  5. The outgoing edge of vertex 33 with the smallest index is edge 11, so the piece moves to vertex 11 through this edge.
  6. The outgoing edge of vertex 11 with the smallest index is edge 22, so the piece moves to vertex 22 through this edge.
  7. The outgoing edge of vertex 22 with the smallest index is edge 33, so the piece moves to vertex 33 through this edge. At this time, edge 33 has been traversed 22 times and is deleted from the graph.
  8. The outgoing edge of vertex 33 with the smallest index is edge 11, so the piece moves to vertex 11 through this edge.
  9. The piece stops at vertex 11, so the destination of this operation is vertex 11.

In the second operation, the piece moves as follows:

  1. Place a piece on vertex 66.
  2. The outgoing edge of vertex 66 with the smallest index is edge 44, so the piece moves to vertex 33 through this edge. At this time, edge 44 has been traversed 11 time and is deleted from the graph.
  3. The outgoing edge of vertex 33 with the smallest index is edge 11, so the piece moves to vertex 11 through this edge. At this time, edge 11 has been traversed 11 time and is deleted from the graph.
  4. The piece has already moved 77 steps, so it stops at vertex 11. Vertex 11 is the destination of this operation.

In the third operation, the piece moves as follows:

  1. Place a piece on vertex 66.
  2. Vertex 66 has no outgoing edges, so the piece can only stop at vertex 66. Therefore, the destination of this operation is vertex 66.

[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 nn \le mm \le QQ \le ss \le ww \le Property Score
1 10510^5 1.5×1051.5 \times 10^5 50005000 50005000 101810^{18} 00 8
2 10910^9 50005000
3 10510^5 101810^{18} 11
4 22 1212
5 n1n - 1 33 13
6 nn 44 15
7 n+50n + 50 55 8
8 1.5×1051.5 \times 10^5 00 28

For all test points, it is guaranteed that 1n1051 \leq n\leq 10^5, 1m1.5×1051 \leq m\leq 1.5\times 10^5, 1Q1051 \leq Q \leq 10^5, 1s1091 \leq s \leq 10^9, 1wi10181 \leq w_i \leq 10^{18}.

  • Property 0: no special property.
  • Property 1: the graph is generated randomly, and each edge (u,v)(u, v) appears with probability 1/n21 / n^2.
  • Property 2: it is guaranteed that wi=1018w_i = 10^{18}.
  • 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 nn edges form a “cycle-with-trees” (huan tao shu).

Translated by ChatGPT 5