Data Structures & AlgorithmsHardCodeforces Div 2 C · GATE CS
The directed acyclic graph has the vertices B, C, K, P, Q, R, V, Z and the arcs Z→V, K→R, C→B, K→C, Q→B, Z→B, Z→K, R→V, Q→Z, P→R, K→B, K→V, P→B, R→B, V→B. How many distinct directed paths go from P to B? A path is a sequence of consecutive arcs; a single arc counts as a path.
- A. 2
- B. 4
- C. 5
- D. 3
Correct answer: D. 3
Count with ways[x] = sum of ways[y] over arcs y→x, starting at ways[P] = 1 and processing vertices in a topological order. This gives 3 paths; using only paths of at most two arcs would give 2.
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
- A directed graph has the vertices C, D, F, G, H, I, Q, W and the arcs I→H, I→D, I→F, I→W, Q→D, Q→F, Q→G, Q→C, H→F, H→W, H→C, F→I, F→Q…
- 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…
- 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…