Scrooge Minho 2
InterviewTime limit2sMemory limit512 MB
Given a tree with N cities, place the fewest police stations so that every city and every road is covered, where a station covers its city, its neighbors, and all incident roads.
- Level
Medium7 of 10
- Topics
- Tree, Dynamic programming, DFS, Greedy
- Solved
- No attempts yet
Problem
Minho, known for pinching every coin, rules the country of Cheon, which has cities. To keep the cost of building roads down he built only roads, so between any two cities there is exactly one route along the roads. Every road is bidirectional.
With the cities finished, Minho now turns to police stations. Putting one in every city is too expensive for him, so he will build them in only some cities. He worries that the citizens will revolt if any city or any road is left unwatched.
A police station built in a city watches that city, every city joined to it by a single road, and every road that touches it. So a road is watched when at least one of its two endpoint cities has a station, and a city is watched when the city itself or one of the cities joined to it by a single road has a station.
Find the smallest number of cities that need a police station so that every city and every road is watched.
Input
The first line contains the number of cities ().
Each of the next lines describes one road with two integers and (, ) separated by a space, meaning city and city are joined by a road. The roads always form a tree.
Output
Print the smallest number of cities that need a police station.