### Varun_Shah's blog

By Varun_Shah, history, 3 months ago, ,

Given a tree with N nodes we are required to seperate a connected component with exactly k nodes. You are given queries specifying this k. We need to find the minimum edges to be removed for each query.
First line specifies N.
Next N-1 lines specify edges.
Next line shows Q(number of queries).
Subsequent Q lines contain k for each query.

Constraint:
N <= 3000
Q <= 3000
K <= N

Example:
Input:
5
1 2
1 3
1 4
1 5
3
1
2
4

Output:
1
3
1

• 0

 » 3 months ago, # |   0 Auto comment: topic has been updated by Varun_Shah (previous revision, new revision, compare).
 » 3 months ago, # |   0 I think this is famous problem — Barricades from Poland. You can read it in book 'Looking for Challenges.
•  » » 3 months ago, # ^ |   0 Can you please elaborate if you know...
•  » » » 3 months ago, # ^ |   0 Do you know O(n**3) solution ?
•  » » » » 3 months ago, # ^ |   0 I thought of this... Dp(i, j) = answer to get a connected component for j nodes considering first i children of the current node... And then some how manipulating its values to get current value
•  » » » 3 months ago, # ^ |   0 O(n^3) solution — DP[v][k] = minimum edges to be removed from subtree of v to get a connected component of size k such that v is included. Now to calculate it we iterate through its childs taking some on none nodes from them. To perform that you have to calculate another internal DP something similar to knapsack. Choose nodes from that with cost DP[child][*] and ignore it with cost 1. To see O(n^2) — https://codeforces.com/blog/entry/63257
•  » » 2 months ago, # ^ |   0 can you give link of book?
 » 3 months ago, # |   0 Can you give link to the problem? I would like to submit and check.
•  » » 3 months ago, # ^ |   0
•  » » » 3 months ago, # ^ |   0 Thanks
•  » » 3 months ago, # ^ |   0 It was in some contest and it's link has expired...
•  » » 2 months ago, # ^ |   0 not in running contest.. can you provide solution?
•  » » » 2 months ago, # ^ |   0 this explains everything. Please also read the blog linked in the comment.