Paths from 0 (undirected)
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 an undirected graph, print a path from vertex 0 to every other vertex 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 found by DFS. - 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. - Then there are
mlines, each containing two integersuandvrepresenting an undirected edge connectinguandv(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
1
2
Example Output:
0->1
0->1->3->2
Bình luận