#C4497. Complete Binary Tree Completion

    ID: 48041 Type: Default 1000ms 256MiB

Complete Binary Tree Completion

Complete Binary Tree Completion

Given a tree with (n) nodes and (n-1) edges, your task is to determine the minimum number of additional nodes (and their corresponding connecting edges) required to transform the tree into a complete binary tree.

A complete binary tree is defined as a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible. Mathematically, a complete binary tree of height (h) contains (2^h - 1) nodes. Therefore, for a given tree with (n) nodes, compute the smallest non-negative integer (k) such that (n + k = 2^h - 1) for some integer (h). If the tree already meets the complete binary tree condition, output (0).

inputFormat

Input is read from standard input (stdin). The first line contains an integer (n) representing the number of nodes in the tree. Each of the following (n-1) lines contains two space-separated integers (u) and (v) that denote an edge in the tree. In the case of a single-node tree, the input consists only of one line with the integer (1).

outputFormat

Output to standard output (stdout) a single integer representing the minimum number of additional nodes (and connecting edges) required to complete the tree as a complete binary tree.## sample

7
1 2
1 3
2 4
2 5
3 6
3 7
0