Full Binary Tree
Time limit5sMemory limit512 MB
Given a tree with up to 15 nodes, delete as few nodes as possible so the remaining nodes form a full binary tree for some choice of root.
- Level
Medium5 of 10
- Topics
- Tree, Dynamic programming, Brute force
- Solved
- No attempts yet
Problem
A tree is a connected graph with no cycles.
A rooted tree is a tree in which one vertex is chosen as the root. In a rooted tree, if and are joined by an edge 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 node has exactly children or children.
You are given a tree with nodes, numbered from to . You may delete some of the nodes. Deleting a node also deletes every edge attached to it. Delete as few nodes as possible so that the remaining nodes form a full binary tree for some choice of root among them.
Input
The first line contains the number of test cases . test cases follow. The first line of each test case contains the number of nodes . Each of the next lines contains two space separated integers and , meaning that has an undirected edge between and .
Limits
- The edges of each test case always form a connected tree.
Output
For each test case, print one line in the form Case #x: y, where is the test case number starting from and is the minimum number of nodes to delete from to make a full binary tree.
Hint
In the first sample test case, is already a full binary tree with node as the root, so nothing has to be deleted.
In the second test case, deleting nodes and leaves a full binary tree rooted at node .
In the third test case, deleting node leaves a full binary tree rooted at node . Deleting node and rooting at node works as well.