Paths from 0

Xem dạng PDF

Gửi bài giải

Điểm: 1,00 (OI)
Giới hạn thời gian: 1.0s
Giới hạn bộ nhớ: 256M
Input: stdin
Output: stdout

Dạng bài
Ngôn ngữ cho phép
C, C++, Java, Kotlin, Pascal, PyPy, Python, Scratch

Given a directed graph, print a path from vertex 0 to every other vertex in the graph using the Depth-First Search (DFS) algorithm. When multiple vertices can be visited, the smallest numbered vertex has higher priority.

For each query in q queries:

  • If there exists a path from vertex 0 to vertex x, print one such path.
  • If no path exists, print -1.

Input

  • The first line contains two integers n, m and q (n ≤ 10^5, m ≤ 2 × 10^5, q ≤ 10^2), representing the number of vertices, edges and queries.
  • The next m lines each contain two integers u and v, representing a directed edge from u to v (0 ≤ u, v < n).
  • The next q lines each contain an integer x, representing the destination vertex (0 ≤ x < n)

    Output

For each query, print one line. If x is reachable from vertex 0, print the vertices on a path from 0 to x separated by spaces. Otherwise, print -1.

Example Input

5 3 2
0 1
1 2
3 4
3
2

Example Output

-1
0 1 2

Explanation

  • Vertex 2 is reachable through the path 0 → 1 → 2.
  • Vertices 3 is reachable from vertex 0, so -1 is printed.

Bình luận

Hãy đọc nội quy trước khi bình luận.


Không có bình luận tại thời điểm này.