Data Structures & AlgorithmsHardGATE CS · Codeforces Div 2 B
A tree has vertices D, H, J, M, Q, R, U, W and weighted edges (weights in brackets): D–H (9), H–Q (5), H–W (5), Q–M (1), D–U (8), U–J (2), M–R (2). What is the largest possible distance between two vertices, where distance is the sum of edge weights on the unique path?
- A. 17
- B. 28
- C. 27
- D. 26
Correct answer: C. 27
Two DFS passes (or a DP over subtrees) give the weighted diameter 27. The farthest distance from D is only 17, and counting edges instead of weights gives 6.
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 the vertices F, G, Q, S, U has the edges U–G, S–Q, F–Q, U–F, G–S, U–S, S–F, G–Q, U–Q. How many spanning…
- A tree has the vertices A, G, H, I, L, O, P, T, U, Z and the edges G–T, G–U, U–H, H–O, O–Z, Z–P, P–A, A–I, P–L. A set D dominates the tree…
- A grid has 8 rows and 6 columns (rows listed top to bottom; '.' is an open cell, '#' a wall): ...#.. ..##.# ...#.. #..#.. ..###. .#.#…
- A tree on the vertices B, N, P, Q, W, Z has the arcs B→W, N→B, P→N, B→Z, B→Q (each undirected edge is directed as shown). You may reverse…
- An undirected graph has the vertices C, F, M, N, Q, V, X, Z and the edges V–M, V–C, V–N, F–M, F–C, X–Z, X–Q, X–M, Z–M, Z–N, Q–M, M–C. What…
- A grid has 7 rows and 7 columns (top to bottom; '.' open, '#' blocked): .....#. .#..##. ....... .###... ....... #...... ....#.. You may…