Protect the Pollen!
InterviewTime limit5sMemory limit1024 MB
Choose flower families so that no two adjacent flowers in the tree are both chosen, with at most S bees total, maximizing total pollination power.
- Level
Medium5 of 10
- Topics
- Tree, Dynamic programming
- Solved
- No attempts yet
Problem
The Flariana flowers and the bumblebees form one of the nicest partnerships in the rainforest. In spring, several flowers bloom and start producing pollen. Special vines form a network of bridges between the flowers. Using the vines, there is exactly one way to get from each flower to any other flower. Every flower has a family of bees on it. This family protects all of the vines that touch that flower, so every vine is protected by two families. The family on flower has bees and a pollination power of . One day, a bee scout reported a new flower patch over the hill, and it needs a group of bees to help pollinate it. As the bee queen, you select a set of families to send on the mission. For every vine, at least one of the two families protecting it must stay behind, so the vine remains protected. All bees in the selected families must go. You can send at most bees on the mission in total. Find the largest total pollination power you can send on the mission.
Input
The first line contains the integers (), the number of flowers, and (), the maximum number of bees you can send. The flowers are numbered 1 to . The next lines describe the families. Each line contains two integers (), the number of bees in the family, and (), the pollination power of the family. The last lines describe the vines. Each line contains two distinct integers () and (), meaning there is a vine between flowers and .
Output
Print the largest total pollination power that you can send on the mission.