Data Structures & AlgorithmsVery hardCodeforces Div 2 C · GATE CS
The directed acyclic graph has the vertices A, B, C, G, J, O and the arcs J→C, G→C, J→O, J→A, G→A, J→B, B→C, O→B. How many distinct directed paths go from J to C? A path is a sequence of consecutive arcs; a single arc counts as a path.
- A. 2
- B. 5
- C. 3
- D. 4
Correct answer: C. 3
Count with ways[x] = sum of ways[y] over arcs y→x, starting at ways[J] = 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 tree has vertices with weights in brackets: T[6], C[15], K[8], Z[3], U[2], V[2], N[14], G[14], F[1], H[7]. The edges are T–C, T–K, K–Z…
- The directed acyclic graph has the vertices D, E, J, L, N, Q, S, Z and the arcs D→Q, Z→N, N→Q, Q→S, D→J, E→S, L→Q, E→Q, D→N, E→N, J→Q…
- A connected undirected graph has the vertices A, D, E, F, H, J, M, Z and the edges J–Z, J–D, J–F, J–M, Z–F, Z–H, Z–E, D–A, D–F, A–E, F–E…
- On an 8×8 chessboard, rows and columns are numbered 0 to 7, and squares are written (row, column). A knight moves in the usual L-shape…
- An undirected graph has the vertices C, D, E, L, M, S, U and the edges U–E, U–S, U–C, D–L, D–C, D–M, L–S, L–M, E–S, E–M, C–M. What is the…
- An undirected graph has the vertices D, E, H, M, O, P, U, V and the edges H–O, H–M, H–D, H–V, O–E, O–V, O–P, M–U, M–V, E–D, E–U, D–U, D–V…