Full Binary Tree (GCJ2014 round1a PB)

Source: Internet
Author: User

Last year, Google Code jam the first round of the B, I did not want to understand, nor found a problem. There is no way to download the code of the great God to see. Feel the gap is great, others use 10 minutes not to make. The title is described below, I will not write translation, test instructions very clear. Because of the wall, I still put the title below it!

topic content
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 so Y is a child of X if x are closer to the root than Y (in Other words, the shortest path from the root to X are shorter than the shortest path from the root to Y).

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

You is given a tree G with N nodes (numbered from 1 to N). You is allowed to delete some of the nodes. When a node was deleted, the edges connected to the deleted node was also deleted. Your task is to delete as few nodes as possible so, 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 all test case contains a single integer N, the number of nodes in the tree. The following N-1 lines each one would contain the space-separated Integers:xi Yi, indicating that G contains an undirecte D 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 th e 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 would 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, the "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 is the root of a full binary tree.

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

Test instructions is to give you a lot of pairs of points, each pair of points between the non-edge. The input ensures that the diagram is connected, not a forest. The topic asks you to remove some points to make this diagram a complete binary tree. The total binary tree refers to the number of child nodes of any node being 0 or 2. It is important to note that the point-to-parent relationship is not deterministic, which is the difficulty of the topic.
The main thing I think about this problem is the structure of the tree. The topic guarantees that there is no isolated point set, so the first step in thinking about this problem is to forget what the binary tree is, and take the input of the question as an no-go graph or a parent-child relationship on any side. A very clear conclusion is that for a given graph, a tree can be constructed with any node as its root, and the tree is unique regardless of the order of the sibling nodes. Of course this tree is probably not a binary tree.
Then the idea of the problem is clear, and the minimum cost of forming a two-fork tree is solved by the tree with each vertex as its root. Then find the minimum value output. The principle of finding the minimum cost is to read the note! The bottom is someone else's code, know the logic code is easy to understand.

#include <stdio.h>#include <algorithm>#include <vector>using namespace STD; vector<int>e[1010];intN, Res, d[1010];//The key point of the topic lies in the non-direction of the five-ring diagram, the title of the figure no outliersvoidDFS (intAintPAR) {//Any form of placement can ensure that the structure of the tree is correct, but there is no guarantee that the two-fork tree    intI, CNT =0;///Sub-nodes more than two must select the minimum cost of the two sub-tree, delete the remaining subtreeD[a] =1;/////byte points less than two vertices are all removed from the child nodes     for(i =0; I < e[a].size (); i++) {if(e[a][i]! = par)            {DFS (E[a][i], a);        cnt++; }    }if(CNT <=1)return;intM1 =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;}//voidDo (intA) {intI DFS (A,-1);if(Res > N-d[a]) Res = N-d[a];}intMain () {Freopen ("Input.txt","R", stdin); Freopen ("Output.txt","W", stdout);intI, TC, T, A, B;scanf("%d", &AMP;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 (); }}

Full Binary Tree (GCJ2014 round1a PB)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.