Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div 2 B
A tree has vertices with weights in brackets: T[6], C[15], K[8], Z[3], U[2], V[2], N[14], G[14], F[1], H[7]. The edges are T–C, T–K, K–Z, C–U, C–V, C–N, C–G, N–F, V–H. 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. 31
- B. 41
- C. 27
- D. 46
Correct answer: D. 46
A tree DP (take a vertex and forfeit its children, or keep the children) gives 46. Greedily taking the heaviest vertex that conflicts with nothing gives only 31.
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
- The directed acyclic graph has the vertices D, E, J, L, N, Q, S, Z and the arcs D→Q, Z→N, N→Q, Q→S, D→J, E→S, L→Q, E→Q, D→N, E→N, J→Q…
- A connected undirected graph has the vertices A, D, E, F, H, J, M, Z and the edges J–Z, J–D, J–F, J–M, Z–F, Z–H, Z–E, D–A, D–F, A–E, F–E…
- On an 8×8 chessboard, rows and columns are numbered 0 to 7, and squares are written (row, column). A knight moves in the usual L-shape…
- An undirected graph has the vertices C, D, E, L, M, S, U and the edges U–E, U–S, U–C, D–L, D–C, D–M, L–S, L–M, E–S, E–M, C–M. What is the…
- An undirected graph has the vertices D, E, H, M, O, P, U, V and the edges H–O, H–M, H–D, H–V, O–E, O–V, O–P, M–U, M–V, E–D, E–U, D–U, D–V…
- An undirected connected graph on the vertices B, H, O, P, S has the edges H–S, O–B, S–P, H–P, S–B, B–P, H–B. How many spanning trees does…