Data Structures & AlgorithmsHardCodeforces Div 2 C · GATE CS
A tree has vertices A, C, G, J, N, Q, X, Y and weighted edges (weights in brackets): Y–Q (2), Y–X (5), Y–J (2), J–C (2), C–A (8), X–G (7), X–N (4). What is the largest possible distance between two vertices, where distance is the sum of edge weights on the unique path?
- A. 5
- B. 12
- C. 24
- D. 22
Correct answer: C. 24
Two DFS passes (or a DP over subtrees) give the weighted diameter 24. The farthest distance from Y is only 12, and counting edges instead of weights gives 5.
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
- Two unmarked jugs hold 5 litres and 7 litres. Allowed operations: fill a jug completely, empty a jug completely, or pour one jug into the…
- A connected undirected graph has the vertices B, H, J, P, Q, V, Z and weighted edges (weights in brackets): J–H (11), Q–P (16), B–P (9)…
- An undirected graph on 5 vertices has the edges C–J, C–D, C–P, J–D, J–P, D–P, P–Z. How many simple paths (no repeated vertex) go from C to…
- Consider this function: python def g(n): if n < 2: return n if n % 2 == 0: return g(n - 3) + g(n - 3) return g(n - 3) - 1 What value does…
- Starting from the integer 14, one operation either adds 1, subtracts 1, or doubles the current value. Values may be any non-negative…
- An undirected graph has the vertices A, H, I, M, R, U, Y, Z and the edges M–Y, M–Z, M–I, M–U, M–H, A–U, A–H, R–Z, R–H, Y–Z, Y–I, Z–I, Z–H…