# Full Binary Tree(GCJ2014 round1A PB)

A tree is a connected graph with no cycles.

A rooted tree is a tree in which one special vertex is called the root. If there is an edge between X and Y in a rooted tree, we say that Y is a child of X if X is closer to the root than Y (in other words, the shortest path from the root to X is shorter than the shortest path from the root to Y).

A full binary tree is a rooted tree where every node has either exactly 2 children or 0 children.

You are given a tree G with N nodes (numbered from 1 to N). You are allowed to delete some of the nodes. When a node is deleted, the edges connected to the deleted node are also deleted. Your task is to delete as few nodes as possible so that the remaining nodes form a full binary tree for some choice of the root from the remaining nodes.

Input

The first line of the input gives the number of test cases, T. T test cases follow. The first line of each test case contains a single integer N, the number of nodes in the tree. The following N-1 lines each one will contain two space-separated integers: Xi Yi, indicating that G contains an undirected edge between Xi and Yi.

Output

For each test case, output one line containing “Case #x: y”, where x is the test case number (starting from 1) and y is the minimum number of nodes to delete from G to make a full binary tree.

Limits

1 ≤ T ≤ 100.
1 ≤ Xi, Yi ≤ N
Each test case will form a valid connected tree.

Small dataset

2 ≤ N ≤ 15.
Large dataset

2 ≤ N ≤ 1000.
Sample

Input

Output

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

Case #1: 0
Case #2: 2
Case #3: 1

In the first case, G is already a full binary tree (if we consider node 1 as the root), so we don’t need to do anything.

In the second case, we may delete nodes 3 and 7; then 2 can be the root of a full binary tree.

In the third case, we may delete node 1; then 3 will become the root of a full binary tree (we could also have deleted node 4; then we could have made 2 the root).

#include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
vector<int>E[1010];
int n, Res, D[1010];      //题目的关键点在于无向五环图，题目的图没有孤立点
void DFS(int a, int par){ //任意形式的摆放都可以保证树的结构正确，不过不能保证是二叉树
int i, cnt = 0;         //子节点多于两个的就必须选择删减代价最小的两个子树，把其余子树删除掉
D[a] = 1;               //字节点少于两个的顶点的子节点全部删除掉
for (i = 0; i < E[a].size(); i++){
if (E[a][i] != par){
DFS(E[a][i], a);
cnt++;
}
}
if (cnt <= 1)return;
int M1 = 0, M2 = 0;
for (i = 0; i < E[a].size(); i++){
if (E[a][i] != par){
if (M1 < D[E[a][i]]){
M2 = M1; M1 = D[E[a][i]]; //
}
else if (M2 < D[E[a][i]]){
M2 = D[E[a][i]];
}
}
}
D[a] = M1 + M2 + 1;
}    //
void Do(int a){
int i;
DFS(a, -1);
if (Res > n - D[a]) Res = n - D[a];
}
int main()
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int i, TC, T, a, b;
scanf("%d", &TC);
for (T = 1; T <= TC; T++){
printf("Case #%d: ", T);
scanf("%d", &n);
for (i = 1; i < n; i++){
scanf("%d%d", &a, &b);
E[a].push_back(b);
E[b].push_back(a);
}
Res = n;
for (i = 1; i <= n; i++){
Do(i);
}
printf("%d\n", Res);
for (i = 1; i <= n; i++)E[i].clear();
}
}