BFS 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. When there is more than one vertex that can be visited next, prioritize visiting the smaller vertex first.
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:
4 4 2
0 3
3 2
1 0
0 1
2
3
Example Output:
0 3 2
0 3
Bình luận