Under construction

Still being built. Things may change

Full Binary Tree (Large)

Time limit5sMemory limit512 MB

Summary
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 XX and YY and the shortest path from the root to XX is shorter than the shortest path from the root to YY, then YY is a child of XX.

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 GG with NN vertices, numbered 11 through NN. 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 TT. The first line of each test case contains one integer NN, the number of vertices of the tree. Each of the next N−1N-1 lines contains two space separated integers XiX_i and YiY_i, meaning that GG has an undirected edge between XiX_i and YiY_i.

Limits

  • 1≤T≤1001 \le T \le 100
  • 2≤N≤10002 \le N \le 1000
  • 1≤Xi,Yi≤N1 \le X_i, Y_i \le N
  • 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 GG to leave a full binary tree.

Hint

In the first sample test case GG 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.

Examples2

  1. Example 1

    Input
    3
    3
    2 1
    1 3
    7
    4 5
    4 2
    1 2
    3 1
    6 4
    3 7
    4
    1 2
    2 3
    3 4
    
    Expected output
    Case #1: 0
    Case #2: 2
    Case #3: 1
    
  2. Example 2

    Input
    1
    2
    1 2
    
    Expected output
    Case #1: 1