Under construction

Still being built. Things may change

High-quality Tree

Time limit3sMemory limit1024 MB

Summary
Given a rooted binary tree, find the fewest leaves to remove so that every subtree has left and right depths differing by at most one.
Level

Medium6 of 10

Topics
Tree, Dynamic programming
Solved
No attempts yet

Statement

The binary search tree is one of the most useful data structures in computer science. Many methods exist to keep them balanced, such as tree rotations (as in AVL trees) or randomness (as in treaps). These methods are all somewhat slow and complicated.

Rob the computer scientist has invented a new method to keep rooted binary trees balanced. It is much better than the current state of the art. The main idea is to repeatedly perform an operation Rob calls a robbery. One robbery takes a leaf from the tree and removes it. By applying robberies at the right times, Rob keeps a tree balanced in O(1)\mathcal{O}(1) amortized time.

Some people have criticised Rob's revolutionary discovery, saying that removing elements from the tree makes the algorithm incorrect. Rob does not agree that this is a big problem. If you plan to store 2⋅1052 \cdot 10^5 numbers, you probably do not need all of them. Rob accepts the criticism and decides to find a way to minimise the number of robberies.

Figure H.1: Illustration of Sample Input 2. The tree becomes strongly balanced after removing the three vertices marked in red (44, 55, and 1010), which is the minimum number of vertices that need to be removed to make the tree strongly balanced.

You are given a rooted binary tree with nn vertices. The vertices are numbered from 11 to nn, and vertex 11 is the root. Your task is to find the minimum number of robberies to make the tree strongly balanced, meaning that all of its subtrees are balanced. A rooted binary tree is balanced if the depths of its left and right subtrees differ by at most one. Recall that a robbery removes a leaf, and doing so may turn its parent into a leaf.

Input

The input consists of:

  • One line with an integer nn (1≤n≤2⋅1051 \leq n \leq 2 \cdot 10^5), the number of vertices in the tree.
  • n−1n-1 lines, each with two integers uu and vv (1≤u,v≤n1 \leq u, v \leq n, u≠vu\neq v), an edge between vertices uu and vv.

The given edges form a valid binary tree with vertex 11 as the root. The edges can appear in any order and are undirected: uu may be the parent of vv, or the other way around.

Output

Output the minimum number of leaves you need to remove to make the tree strongly balanced.

Examples2

  1. Example 1

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

    Input
    12
    1 2
    2 3
    3 4
    3 5
    1 6
    6 7
    7 8
    7 9
    9 10
    6 11
    11 12
    
    Expected output
    3