Data Structures & AlgorithmsHardGATE CS · ICPC regional
A tree has vertices E, H, I, K, L, M, V, Z and weighted edges (weights in brackets): I–E (5), I–L (9), I–H (9), E–K (6), K–Z (7), E–M (1), I–V (5). What is the largest possible distance between two vertices, where distance is the sum of edge weights on the unique path?
- A. 28
- B. 27
- C. 18
- D. 26
Correct answer: B. 27
Two DFS passes (or a DP over subtrees) give the weighted diameter 27. The farthest distance from I is only 18, and counting edges instead of weights gives 4.
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 acyclic graph has the vertices D, E, I, Q, R, U, Z and the edges D→R, D→E, Z→D, Z→R, Z→Q, Z→E, R→U, Q→D, I→D. A topological…
- A directed graph has the vertices H, J, M, T, X, Y and the arcs M→T, M→X, T→X, J→Y, X→Y, Y→T, Y→J. How many strongly connected components…
- A tree has the vertices D, E, I, L, O, P, Q, T, W and the edges T–D, D–L, L–Q, Q–E, T–I, E–O, T–P, D–W. A centroid is a vertex whose…
- The directed acyclic graph has the vertices E, H, J, K, S, T, V, W and the arcs W→E, S→K, J→H, V→H, J→K, V→W, W→K, E→H, J→T, T→E, V→S…
- Grid with 3 rows and 3 columns (top to bottom). A cell marked G is a gate; '.' is a normal cell: .GG G.. ... Each step, which is a move to…
- A directed acyclic graph has the vertices M, Q, U, V, X, Y, Z and the arcs M→Y, M→Q, M→V, U→Y, U→Z, U→V, Y→Q, Y→Z, Y→V, Q→Z, Q→X, Z→X…