Barricades
Time limit1sMemory limit128 MB
On a tree, for each size k find the minimum number of edges to cut so that some connected component has exactly k vertices and no edges leave it.
- Level
Medium7 of 10
- Topics
- Tree, Dynamic programming, DFS, Implementation
- Solved
- No attempts yet
Problem
Byteland is an island whose cities are connected by two-way roads. The road network is built so that between any pair of cities there is exactly one route (as long as you never turn back). In other words, the cities and roads form a tree.
Hard times have come, and Byteland is preparing for war. The chief strategist wants to set up a special security zone by placing barricades on some roads so that no one can drive across a barricaded road. For the zone to be secure it must satisfy all of the following:
- from every city inside the zone you can reach every other city inside the zone;
- no one can drive from a city outside the zone into a city inside the zone;
- the zone contains exactly cities.
For several values of the strategist wants to know the minimum number of roads that must be barricaded to build a special security zone of exactly cities.
Write a program that reads the road network and the list of queries, and for each query prints to standard output the minimum number of barricades needed to build a special security zone of the requested size.
Input
The first line contains an integer (), the number of cities. Cities are numbered .
Each of the next lines contains two integers and () separated by a single space, describing a road that directly connects cities and . Every pair of cities is joined by at most one direct road.
The next line contains an integer (), the number of queries. Each of the following lines contains one integer (): query asks for a special security zone that contains exactly cities.
Output
Print exactly lines. Line must contain:
- , if a special security zone of exactly cities cannot be built;
- otherwise, the minimum number of roads that must be barricaded to build such a zone.