Data Structures & AlgorithmsHardCodeforces Div 2 C · GATE CS
A tree has vertices with weights in brackets: Y[6], L[12], B[1], X[10], W[6], I[3], N[4], V[14], J[7]. The edges are Y–L, Y–B, L–X, L–W, W–I, L–N, B–V, W–J. 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. 38
- B. 43
- C. 44
- D. 40
Correct answer: C. 44
A tree DP (take a vertex and forfeit its children, or keep the children) gives 44. Greedily taking the heaviest vertex that conflicts with nothing gives only 36.
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 directed graph has the vertices D, F, M, O, S and the arcs M→D, M→O, D→O, D→F, O→S, F→O, S→M. Count the directed Hamiltonian paths…
- On an 7×7 chessboard, rows and columns are numbered 0 to 6, and squares are written (row, column). A knight moves in the usual L-shape…
- A directed acyclic graph has the vertices A, F, J, K, P, R, S and the edges P→A, P→F, A→F, J→F, J→S, J→R, S→F. A topological order lists…
- A counter starts at 1. Each step, you may add 1, add 2, or double the counter. Each step must stay at or below 23. Two sequences of steps…
- Bellman–Ford is run from source S on the directed graph whose arcs (weights in brackets) are listed in the order they are relaxed: S→E…
- Cities J, O, Q, V, X, Y are joined by roads with these travel costs: X–J 4, X–Q 5, X–Y 7, X–V 6, X–O 2, J–Q 8, J–Y 4, J–V 5, J–O 9, Q–Y…