Data Structures & AlgorithmsHardCodeforces Div 2 C · GATE CS
A tree has vertices with weights in brackets: O[11], H[15], M[14], Z[13], Q[2], V[7], L[15], T[3]. The edges are O–H, O–M, M–Z, M–Q, Q–V, O–L, M–T. A set of vertices is independent if no edge joins two of its members. What is the maximum total weight of an independent set?
- A. 51
- B. 53
- C. 52
- D. 29
Correct answer: B. 53
A tree DP (take a vertex and forfeit its children, or keep the children) gives 53. Greedily taking the heaviest vertex that conflicts with nothing gives only 51.
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 grid has 4 rows and 4 columns with these values (top to bottom): 7416 6762 2894 3773 A path moves between side-adjacent cells (up, down…
- A connected undirected graph has the vertices F, I, L, R, S, W, Z and the edges R–S, R–I, R–W, S–L, S–Z, I–F, L–Z. Parallel edges are…
- An undirected graph on 7 vertices has the edges U–Z, U–B, U–J, Z–G, Z–N, G–P, P–B, P–N, P–J, N–J. How many simple paths (no repeated…
- A connected undirected graph has the vertices A, D, J, M, S, Z and the edges M–Z, Z–S, Z–J, Z–A, S–D, S–A, D–A. How many bridges does it…
- A flow network has source S and sink T, with directed arcs and capacities in brackets: S→E (4), S→T (9), C→D (8), C→T (3), B→E (6), B→T…
- A tree has the vertices A, E, G, I, K, L, N, O, W and the edges G–W, W–O, O–E, E–L, G–I, G–A, A–K, O–N. A centroid is a vertex whose…