Data Structures & AlgorithmsVery hardCodeforces Div 2 D · ICPC regional
A tree has vertices with weights in brackets: D[15], C[15], R[7], L[9], S[1], Z[3], B[13], H[13], F[6]. The edges are D–C, D–R, R–L, D–S, D–Z, D–B, Z–H, S–F. 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. 55
- B. 56
- C. 57
- D. 43
Correct answer: B. 56
A tree DP (take a vertex and forfeit its children, or keep the children) gives 56. Greedily taking the heaviest vertex that conflicts with nothing gives only 43.
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 tree on the vertices C, F, H, L, P, V, W has the arcs V→P, F→P, P→C, F→H, V→L, W→H (each undirected edge is directed as shown). You may…
- Each cell of a grid with 3 rows and 4 columns has an arrow: R = right, L = left, U = up, D = down. RRLL URUU RRUD From a cell you may move…
- A counter starts at 1. Each step, you may add 1, add 2, or double the counter. Each step must stay at or below 18. Two sequences of steps…
- A pile holds p stones. In one move a player removes exactly s stones, where s is one of {2, 3, 4} and s ≤ the pile size. The player who…
- A connected undirected graph has the vertices B, F, K, N, U, W and weighted edges (weights in brackets): F–U (13), F–N (4), N–U (17), B–W…
- An undirected connected graph on 5 vertices has the edges (weights in brackets, all distinct): Y–K (11), S–K (2), Q–D (10), D–K (14), Y–S…