Data Structures & AlgorithmsHardGATE CS · ICPC regional
A tree has vertices A, D, E, J, L, Q, T, W, X and weighted edges (weights in brackets): L–T (2), T–D (6), D–W (9), T–Q (6), Q–J (7), L–X (6), T–E (9), W–A (8). What is the largest possible distance between two vertices, where distance is the sum of edge weights on the unique path?
- A. 34
- B. 5
- C. 25
- D. 36
Correct answer: D. 36
Two DFS passes (or a DP over subtrees) give the weighted diameter 36. The farthest distance from L is only 25, 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
- Each cell of a grid with 3 rows and 4 columns has an arrow: R = right, L = left, U = up, D = down. DLUL RUUR DLRD From a cell you may move…
- A directed graph with no directed cycles has arcs u→v (weight) : A→U (8), Z→U (1), Z→I (1), E→U (-2), E→I (2), U→C (4), U→I (5), C→I (-2)…
- A connected undirected graph has the vertices F, G, O, P, R, U, Y and the edges P–O, P–G, P–F, O–U, O–R, R–G, R–Y, Y–F. How many bridges…
- On an 6×6 chessboard, rows and columns are numbered 0 to 5, and squares are written (row, column). A knight moves in the usual L-shape…
- Grid with 3 rows and 3 columns (top to bottom). A cell marked G is a gate; '.' is a normal cell: ... ..G GG. Each step, which is a move to…
- A grid has 6 rows and 5 columns (top to bottom; '.' open, '#' blocked): ...#. .#..# ..... ##.#. ..... ..... You may only move right or…