Watching Cowflix
Time limit3sMemory limit1024 MB
Given a tree with marked nodes, find for each k from 1 to N the minimum total cost of disjoint connected components covering all marked nodes, where a component of size d costs d+k.
- Level
Hard8 of 10
- Topics
- Tree, Dynamic programming, Divide and conquer
- Solved
- No attempts yet
Statement
Bessie likes watching shows on Cowflix, and she watches them in different places. Farmer John's farm is a tree with () nodes. Each node is either a place where Bessie watches Cowflix or a place where she does not. At least one node is a place where Bessie watches Cowflix.
Cowflix is introducing a new subscription model to combat password sharing. Under the new model, you choose a connected component of size in the farm, and you pay moonies for an account that works in that component. Formally, choose a set of disjoint connected components so that every node where Bessie watches Cowflix is contained in some . The cost of the set is , where is the number of nodes in . Nodes where Bessie does not watch Cowflix do not have to be in any .
Bessie is worried the new model may be too expensive for her viewing habits, and she is considering switching to Mooloo. To help her decide, calculate the minimum amount she must pay Cowflix to keep her viewing habits. Cowflix has not announced , so calculate the answer for every integer from to .
Input
The first line contains . The second line contains a bit string , where if Bessie watches Cowflix at node . Then lines follow, each with two integers and (), denoting an edge between and in the tree.
Output
Print the answers for each from to , one per line.