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
0to vertexx, print one such path. - If no path exists, print
-1.
Input
- The first line contains two integers
n,mandq(n ≤ 10^5,m ≤ 2 × 10^5,q ≤ 10^2), representing the number of vertices, edges and queries. - The next
mlines each contain two integersuandv, representing a directed edge fromutov(0 ≤ u, v < n). - The next
qlines each contain an integerx, 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
2is reachable through the path0 → 1 → 2. - Vertices
3is reachable from vertex0, so-1is printed.
Bình luận