Subway
Time limit3sMemory limit128 MB
Given a tree with n stations, choose l non-branching paths (routes) to cover as many distinct vertices as possible.
- Level
Hard8 of 10
- Topics
- Tree, Dynamic programming, Greedy
- Solved
- No attempts yet
Problem
A certain city has been building its subway for a long time. Its finances were mismanaged and the costs so badly underestimated that no money was left to buy trains. As a result far too many stations were built and only some of the planned tunnels were finished, barely enough to keep every pair of stations connected. Each tunnel is bidirectional, and the number of tunnels is exactly one less than the number of stations. With the money that was left, only a handful of trains could be bought.
To save face, the board of directors asked you to plan the subway routes so that as many stations as possible are served. Each train runs on one specified route. A route cannot branch (no three tunnels meeting at a single station may belong to the same route). Different routes may share the same station or the same tunnel.
Write a program that:
- reads a description of the tunnel system and the number of subway lines to be planned from standard input,
- computes the maximum number of stations that can be covered by the given number of subway lines,
- writes the result to standard output.
Input
The first line contains two integers and separated by a single space (, ). Here is the number of stations and is the number of subway lines to be planned. The stations are numbered from to .
Each of the next lines contains two distinct integers separated by a single space. In line , the integers and () are the numbers of the two stations joined by the -th tunnel.
Output
Print a single integer: the maximum number of stations that can be covered by the train routes.
Hint

The figure shows the tunnel system (with the subway routes marked) in one of the optimal configurations.