*[hackerrank]Tree Covering
https://www.hackerrank.com/contests/illuminati/challenges/tree-covering
这道题先是在上次交流讨论了一下,然后两位百度的朋友先写完代码share出来了,觉得是道很好的题,就做了一下。https://gist.github.com/coder32167/6964331 https://gist.github.com/snakeDling/6965299
基本思想是贪心。根据题意,所选的点必然是叶子节点,那么首先找出树的直径,直径上的这两个点都要。找第三个点的时候,遍历所有的点,找出到直径(上任意一点)距离最小的叶子节点,接着以此类推找第四个点。
贪心可行的依据可直观的这么看,假设AB是树的直径,那么从树中任意一其他叶子X出发寻找最长路,要么是AX,要么是BX。这个是广为证明的一个结论,已经用于寻找直径了。
接下来就是实现,怎么求第三个点,第四个点呢。答案是递归BFS,递归到叶子节点时cover是1,然后往上回溯。对任意父节点,取子节点中cover数最大的加一,剩下的都放入vector中,最后排序。(或者放入堆中也可,就不用最后排序了。)
最后依次把这些branch加回去。过程如下示意图展示:就是先取直径上任意一点,然后根据BFS得到的排序的branch长度,一个一个按顺序将剩余段加入回去。

#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue> using namespace std; int get_root(vector<vector<int>> &tree, int root) {
int n = tree.size();
vector<bool> visit(n, false);
vector<int> len(n);
queue<int> que; int max_len = 1;
que.push(root);
len[root] = 1;
while (!que.empty()) {
int node = que.front();
que.pop();
if (visit[node])
continue;
visit[node] = true;
int m = tree[node].size();
if (len[node] > max_len) {
root = node;
max_len = len[node];
}
for (int i = 0; i < m; i++) {
que.push(tree[node][i]);
len[tree[node][i]] = len[node] + 1;
}
}
return root;
} int collect_branch(vector<vector<int>> &tree, int root, vector<int> &branch, vector<bool> &visit) {
visit[root] = true;
int m = tree[root].size();
vector<int> result;
for (int i = 0; i < m; i++) {
if (visit[tree[root][i]])
continue;
int len = collect_branch(tree, tree[root][i], branch, visit);
result.push_back(len);
}
if (result.size() == 0)
return 1;
sort(result.begin(), result.end());
int ret = result.back();
result.pop_back();
for (int i = 0; i < result.size(); i++) {
branch.push_back(result[i]);
}
return ret + 1;
} int main()
{
int n;
scanf("%d", &n);
vector<vector<int>> tree(n+1);
for (int i = 1; i < n; i++) {
int a, b;
scanf("%d", &a);
scanf("%d", &b);
tree[a].push_back(b);
tree[b].push_back(a);
} int root = get_root(tree, 1);
vector<int> branch;
vector<bool> visit(n+1, false);
int main_branch = collect_branch(tree, root, branch, visit);
branch.push_back(main_branch);
sort(branch.begin(), branch.end()); printf("%d\n", 1);
int total = 0;
for (int i = 1; i < n; i++) {
if (branch.size() != 0) {
total += branch.back();
branch.pop_back();
}
printf("%d\n", total);
} return 0;
}
*[hackerrank]Tree Covering的更多相关文章
- hackerrank答案
regex: https://github.com/taiyang-li/hackerrank/tree/master/hackerrank/regex-exercise awk: https://g ...
- Finding Memory Leaks with SAP Memory Analyzer
Introduction There is a common understanding that a single snapshot of the java heap is not enough f ...
- HackerRank "Self Balancing Tree"
Something to learn: Rotation ops in AVL tree does not require recursion. https://github.com/andreima ...
- *[hackerrank]Cut the tree
https://www.hackerrank.com/contests/w2/challenges/cut-the-tree 树分成两部分,求两部分差最小.一开始想多了,后来其实求一下总和,求一下分部 ...
- 【HackerRank】Cut the tree
题目链接:Cut the tree 题解:题目要求求一条边,去掉这条边后得到的两棵树的节点和差的绝对值最小. 暴力求解会超时. 如果我们可以求出以每个节点为根的子树的节点之和,那么当我们去掉一条边(a ...
- 【HackerRank】Utopian tree
The Utopian tree goes through 2 cycles of growth every year. The first growth cycle of the tree occu ...
- HackerRank "Kundu and Tree" !!
Learnt from here: http://www.cnblogs.com/lautsie/p/3798165.html Idea is: we union all pure black edg ...
- HackerRank "The Indian Job"
A sly knapsack problem in disguise! Thanks to https://github.com/bhajunsingh/programming-challanges/ ...
- HackerRank "Array and simple queries" !
The most interesting, flexible and juicy binary tree problem I have ever seen. I learnt it from here ...
随机推荐
- 【转】Java web 编解码
几种常见的编码格式 为什么要编码 不知道大家有没有想过一个问题,那就是为什么要编码?我们能不能不编码?要回答这个问题必须要回到计算机是如何表示我们人类能够理解的符号的,这些符号也就是我们人类使用的语言 ...
- C++学习(四)
一.拷贝构造函数和拷贝赋值运算符1.拷贝构造:用一个已有的对象,构造和它同类型的副本对象——克隆.2.形如class X { X (const X& that) { ... }};的构造函数 ...
- 软件工程——UML简介
UML概述: UML是对OMT(对象建模技术).Booth(Booch方法)以及OOSE(面向对象的软件工程)等记号系统实施统一工作后得到的一种记号系统. UML(Unified Modeling L ...
- OC与Swift的区别五(函数)
13 函数 oc函数定义: 返回值类型 函数名(参数类型 参数名,参数类型 参数名){ } swift 函数定义: func 函数名(参数名:参数类型,参数名:参数类型) -> 返回值类型{ } ...
- Myeclipse配置mybatis的xml自动提示
关于mapper的xml的文件的自动提示 mapper头: <?xml version="1.0" encoding="UTF-8"?><!D ...
- JS 浮点型数字运算(转)
示例: var num1=3.3; var num2=7.17; var ret=parseFloat(num1)+parseFloat(num2); //ret的值为:10.469999999999 ...
- 时间处理得到UTC时间
在工作过程遇到了时间处理的问题,因为需要统一将时间处理按照utc时间进行处理,因此,不能简单的通过系统运行直接得到时间的毫秒数,这样会在不同时区得到的值是不同的. import java.text.P ...
- 九度OJ 1451 不容易系列之一 -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1451 题目描述: 大家常常感慨,要做好一件事情真的不容易,确实,失败比成功容易多了! 做好“一件”事情尚且不易,若 ...
- QT5.0.1在Windows下 出现QApplication: No such file or directory 问题的解决办法
第一个Qt 程序 环境window ,ide qt creator 新建一个 C++ 项目 > 新建一个main.cpp 输入如下代码 #include<QApplication> ...
- ubuntu 安装 桌面 awesome
受了ubuntu 12.04自带的桌面,运行太卡了 http://www.linuxzen.com/awesometmuxgnomedoda-zao-gao-xiao-linuxzhuo-mian-h ...