#P13248. [GCJ 2014 #1A] Full Binary Tree

[GCJ 2014 #1A] Full Binary Tree

题目描述

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 XX and YY in a rooted tree, we say that YY is a child of XX if XX is closer to the root than YY (in other words, the shortest path from the root to XX is shorter than the shortest path from the root to YY).

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

You are given a tree GG with NN nodes (numbered from 11 to NN). 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.

输入格式

The first line of the input gives the number of test cases, TT. TT test cases follow. The first line of each test case contains a single integer NN, the number of nodes in the tree. The following N1N-1 lines each one will contain two space-separated integers: Xi YiX_i\ Y_i, indicating that G contains an undirected edge between XiX_i and YiY_i.

输出格式

For each test case, output one line containing "Case #xx: yy", where xx is the test case number (starting from 11) and yy is the minimum number of nodes to delete from GG to make a full binary tree.

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

提示

Sample Explanation

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

In the second case, we may delete nodes 33 and 77; then 22 can be the root of a full binary tree.

In the third case, we may delete node 11; then 33 will become the root of a full binary tree (we could also have deleted node 44; then we could have made 22 the root).

Limits

  • 1T1001 \leqslant T \leqslant 100.
  • 1Xi,YiN1 \leqslant X_i, Y_i \leqslant N
  • Each test case will form a valid connected tree.

Small Dataset(9 Pts)

  • Time limit: 60 3 seconds.
  • 2N152 \leqslant N \leqslant 15.

Large dataset(21 Pts)

  • Time limit: 120 10 seconds.
  • 2N10002 \leqslant N \leqslant 1000.