Cell Phone Network
InterviewTime limit1sMemory limit128 MB
Given a tree of N pastures, choose the fewest vertices so that every vertex is chosen or adjacent to a chosen one.
- Level
Medium6 of 10
- Topics
- Tree, Dynamic programming, Greedy, DFS
- Solved
- No attempts yet
Problem
Farmer John has decided to give each of his cows a cell phone to encourage their social interaction. To let the cows communicate, he must install cell phone towers on his pastures (conveniently numbered through ).
Exactly pairs of pastures are adjacent, and for any two pastures and there is a sequence of adjacent pastures leading from to . In other words, the pastures form a tree.
Towers can only be placed on pastures. A tower placed on a pasture provides service to that pasture and to every pasture adjacent to it.
Determine the minimum number of towers Farmer John must install so that every pasture receives cell phone service.
Constraint: .
Input
- Line 1: a single integer ().
- Lines 2 to : each line contains two space-separated integers and describing a pair of adjacent pastures (, ).
Output
- A single integer: the minimum number of towers needed so that every pasture receives service.
Hint
The picture below shows one example with pastures whose adjacencies form a tree.
4 2
| |
1--3--5
A tower on pasture serves pastures , and ; adding one more tower on pasture (or ) covers the rest. Since each tower serves itself and its neighbors, the goal is to place towers so that their combined coverage reaches every pasture.