Data Structures & AlgorithmsHardCodeforces Div 2 · ICPC regional
Tree with vertices 0 to 8, root 0, edges 0–1, 1–2, 2–3, 1–4, 4–5, 1–6, 3–7, 6–8 (parent–child). Vertex weights (vertex:weight) 0:18, 1:16, 2:10, 3:11, 4:1, 5:6, 6:1, 7:6, 8:2. Choose a set of vertices with no two adjacent in the tree that maximises the total weight. What is that maximum total weight?
- A. 42
- B. 35
- C. 37
- D. 29
Correct answer: A. 42
Tree DP with take/skip states gives 42. Greedily taking the heaviest vertices first gives 37, and the minimum vertex cover weight 29 is the complement, not the answer.
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
- Weighted tree on vertices 0 to 7 with edges 0–1 (8), 0–2 (4), 0–3 (1), 1–4 (2), 1–5 (2), 5–6 (4), 1–7 (7). What is the largest possible…
- Tree with vertices 0 to 6, root 0, edges 0–1, 0–2, 1–3, 3–4, 4–5, 2–6 (parent–child). Vertex weights (vertex:weight) 0:13, 1:5, 2:12…
- Distinct words ['baab', 'bcb', 'cbaa', 'ccba', 'bbaa', 'ab', 'aac'] are inserted into an empty trie (one node per character, shared…
- Connected undirected graph on vertices A, B, C, D, E, F, G, H with edges A–B (1), A–E (1), A–G (1), C–F (1), C–G (1), D–F (1), D–H (1)…
- Undirected graph on vertices A, B, C, D, E, F with weighted edges A–D (2), A–E (3), A–F (6), B–D (9), B–F (5), C–F (5), D–E (2), E–F (9)…
- Words ['b', 'c', 'babb', 'accc', 'aaa', 'c'] are inserted in this order into an empty trie with one node per character, shared prefixes…