Data Structures & AlgorithmsHardCodeforces Div 2 D · ICPC regional
A directed acyclic graph has the vertices A, D, M, O, Q, U, V and the edges U→M, U→D, U→V, Q→U, Q→O, A→M, A→V, O→U. A topological order lists every vertex once so that each edge goes from an earlier to a later vertex. Which topological order is lexicographically smallest when read as a sequence of letters?
- A. Q O U D A M V
- B. A Q O U D M V
- C. Q O U D A V M
- D. Q O U A M V D
Correct answer: B. A Q O U D M V
Kahn's algorithm that always takes the smallest available vertex (a min-heap) gives A Q O U D M V. A FIFO queue gives A Q O U D M V, which can differ, and a reverse-postorder DFS gives Q O U D A V M.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- An undirected connected graph on 6 vertices has the edges (weights in brackets, all distinct): X–K (3), S–D (11), X–S (24), S–K (13), D–K…
- An undirected graph has the vertices E, F, O, P, Q and the edges Q–E, F–O, F–E, F–P, O–P, E–P. A walk may repeat vertices and edges. How…
- A directed graph has the vertices C, D, G, M, P, W and the arcs D→P, D→W, C→M, C→P, G→D, W→D, W→G. How many strongly connected components…
- A tree has vertices with weights in brackets: O[11], H[15], M[14], Z[13], Q[2], V[7], L[15], T[3]. The edges are O–H, O–M, M–Z, M–Q, Q–V…
- A grid has 4 rows and 4 columns with these values (top to bottom): 7416 6762 2894 3773 A path moves between side-adjacent cells (up, down…
- A connected undirected graph has the vertices F, I, L, R, S, W, Z and the edges R–S, R–I, R–W, S–L, S–Z, I–F, L–Z. Parallel edges are…