Google Code Jam 2014 Round 1 A:Problem B. Full Binary Tree
Problem
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 |
Case #1: 0 |
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).
Google Code Jam 2014 Round 1 A:Problem B. Full Binary Tree的更多相关文章
- Google Code Jam 2014 Round 1 A:Problem C. Proper Shuffle
Problem A permutation of size N is a sequence of N numbers, each between 0 and N-1, where each numbe ...
- Google Code Jam 2014 Round 1 A:Problem A Charging Chaos
Problem Shota the farmer has a problem. He has just moved into his newly built farmhouse, but it tur ...
- Google Code Jam 2014 Round 1B Problem B
二进制数位DP,涉及到数字的按位与操作. 查看官方解题报告 #include <cstdio> #include <cstdlib> #include <cstring& ...
- [C++]Store Credit——Google Code Jam Qualification Round Africa 2010
Google Code Jam Qualification Round Africa 2010 的第一题,很简单. Problem You receive a credit C at a local ...
- Google Code Jam 2010 Round 1C Problem A. Rope Intranet
Google Code Jam 2010 Round 1C Problem A. Rope Intranet https://code.google.com/codejam/contest/61910 ...
- [Google Code Jam (Qualification Round 2014) ] B. Cookie Clicker Alpha
Problem B. Cookie Clicker Alpha Introduction Cookie Clicker is a Javascript game by Orteil, where ...
- [Google Code Jam (Qualification Round 2014) ] A. Magic Trick
Problem A. Magic Trick Small input6 points You have solved this input set. Note: To advance to the ...
- Google Code Jam 2014 资格赛:Problem B. Cookie Clicker Alpha
Introduction Cookie Clicker is a Javascript game by Orteil, where players click on a picture of a gi ...
- [C++]Saving the Universe——Google Code Jam Qualification Round 2008
Google Code Jam 2008 资格赛的第一题:Saving the Universe. 问题描述如下: Problem The urban legend goes that if you ...
随机推荐
- 【NOIP模拟赛】【数学真奇妙】【递推】旅行者问题
旅行者问题 [问题描述] lahub是一个旅行者的粉丝,他想成为一个真正的旅行者,所以他计划开始一段旅行.lahub想去参观n个目的地(都在一条直道上).lahub在起点开始他的旅行.第i个目的地和起 ...
- elasticsearch5.3.0 安装
公司有项目打算用elasticsearch,所以研究了下,目前最新版本5.3.0 安装 1.下载包 https://artifacts.elastic.co/downloads/elasticsea ...
- Android-onInterceptTouchEvent()和onTouchEvent()
老实说,这两个小东东实在是太麻烦了,很不好懂,我自己那api文档都头晕,在网上找到很多资料,才知道是怎么回事,这里总结一下,记住这个原则就会很清楚了: 1.onInterceptTouchEvent( ...
- Ubuntu 16.04服务器版查看IP、网关、DNS(非DHCP)
查看IP ifconfig em1 Link encap:Ethernet HWaddr F0:1F:AF:D6:17:DD inet addr:115.238.54.116 Bcast:115.23 ...
- UBIFS
转:http://www.armadeus.com/wiki/index.php?title=UBIFS This is a preliminary page dealing with the ins ...
- Uri详解之——Uri结构与代码提取
目录(?)[+] 前言:依然没有前言…… 相关博客:1.<Uri详解之——Uri结构与代码提取>2.<Uri详解之二——通过自定义Uri外部启动APP与Notification启动& ...
- hdu 1283 最简单的计算机
水题. .. import java.util.Scanner; public class Main { static int m1, m2; static int r1, r2, r3; publi ...
- C#实现在Form上截取消息的两种方法
比较常用的是重载Form的DefWndProc方法,例如截取鼠标按下的消息: protected override void DefWndProc(ref Message m) { if ( m.Ms ...
- bind域名dns解析及主从服务的配置
bind域名dns解析及主从服务的配置 1.dns解析介绍 人们习惯记忆域名,但机器间互相只认IP地址,域名与IP地址之间是多对一的关系,一个ip地址不一定只对应一个域名,且一个域名只可以对应 ...
- python学习的推荐教程
评注: 还未细看, http://www.crifan.com/files/doc/docbook/python_beginner_tutorial/release/html/python_begin ...