Data Structures & AlgorithmsVery hardGATE CS · ICPC regional
A tree has vertices F, G, J, K, L, M, N, U, X and weighted edges (weights in brackets): N–F (4), N–U (3), U–M (7), N–X (7), U–J (7), U–K (2), K–L (6), X–G (8). What is the largest possible distance between two vertices, where distance is the sum of edge weights on the unique path?
- A. 5
- B. 27
- C. 15
- D. 26
Correct answer: D. 26
Two DFS passes (or a DP over subtrees) give the weighted diameter 26. The farthest distance from N is only 15, 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
- Bellman–Ford is run from source I on the directed graph whose arcs (weights in brackets) are listed in the order they are relaxed: O→I…
- A counter starts at 1. Each step, you may add 1, add 2, or double the counter. Each step must stay at or below 12. Two sequences of steps…
- Consider this recursive function: python def f(n): if n <= 1: return f(n - 2) if n % 3 == 0: f(n - 2) How many times is f called in total…
- An undirected graph has the vertices J, K, M, O, P, Y and the edges P–M, P–Y, P–O, P–K, P–J, M–Y, M–O, Y–K, O–K. What is its chromatic…
- A directed graph has the vertices B, F, I, S and the arcs I→F, I→S, F→I, F→B, S→I, S→F, S→B, B→F, B→S. Count the directed Hamiltonian…
- An undirected graph has the vertices L, O, Q, Y, Z and the edges L–Z, O–Z, O–Y, Q–O, Q–Y, L–Y, L–Q, Y–Z. What is the minimum number of…