Full Binary Tree (Large)
Time limit5sMemory limit512 MB
Delete as few vertices as possible from a given tree so the survivors form a full binary tree with a freely chosen root.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Tree, DFS
- Solved
- No attempts yet
Problem
A tree is a connected graph with no cycles.
A rooted tree is a tree with one vertex chosen as the root. In a rooted tree, if there is an edge between and and the shortest path from the root to is shorter than the shortest path from the root to , then is a child of .
A full binary tree is a rooted tree in which every vertex has exactly 2 children or exactly 0 children.
You are given a tree with vertices, numbered through . You may delete some of the vertices, and deleting a vertex also deletes every edge attached to it. Delete as few vertices as possible so that the vertices left over form a single full binary tree for some choice of root among them.
Input
The first line contains the number of test cases . The first line of each test case contains one integer , the number of vertices of the tree. Each of the next lines contains two space separated integers and , meaning that has an undirected edge between and .
Limits
- The edges of every test case form a connected tree.
Output
For each test case print one line in the form Case #x: y, where x is the test case number starting from 1 and y is the minimum number of vertices you have to delete from to leave a full binary tree.
Hint
In the first sample test case is already a full binary tree with vertex 1 as the root, so nothing has to be deleted.
In the second sample test case, deleting vertices 3 and 7 leaves a full binary tree rooted at vertex 2.
In the third sample test case, deleting vertex 1 leaves a full binary tree rooted at vertex 3. Deleting vertex 4 and rooting at vertex 2 works too.