This page is still under construction.

Parts of this page are still being built. What you see may change.

Cell Phone Network

Interview

Time limit1sMemory limit128 MB

Summary
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 NN pastures (conveniently numbered 11 through NN).

Exactly N−1N-1 pairs of pastures are adjacent, and for any two pastures AA and BB there is a sequence of adjacent pastures leading from AA to BB. 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: 1≤N≤100001 \le N \le 10000.

Input

  • Line 1: a single integer NN (1≤N≤100001 \le N \le 10000).
  • Lines 2 to NN: each line contains two space-separated integers AA and BB describing a pair of adjacent pastures (1≤A,B≤N1 \le A, B \le N, A≠BA \ne B).

Output

  • A single integer: the minimum number of towers needed so that every pasture receives service.

Hint

The picture below shows one example with 55 pastures whose adjacencies form a tree.

   4  2
   |  |
1--3--5

A tower on pasture 33 serves pastures 1,3,41, 3, 4, and 55; adding one more tower on pasture 22 (or 55) 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.

Examples3

  1. Example 1

    Input
    5
    1 3
    5 2
    4 3
    3 5
    
    Expected output
    2
    
  2. Example 2

    Input
    2
    1 2
    
    Expected output
    1
    
  3. Example 3

    Input
    3
    1 2
    2 3
    
    Expected output
    1