*[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 ...
随机推荐
- Errors occurred during the build. Errors running builder 'DeploymentBuilder' on project '项目名'
问题描述: Errors occurred during the build. Errors running builder 'DeploymentBuilder' on project 'myf'. ...
- Dapper.NET - ORM(ibatis.Net)
Dapper.NET使用 1.为什么选择Dapper 2.以Dapper(4.0)为例. 2.1 在数据库中建立几张表. 2.2实体类. 3.使用方法 3.1 一对一映射 3.2 一对多映射 3.3 ...
- 几种JavaScript富应用MVC MVVM框架
Ember.js.Backbone.js.Knockout.js.Spine.js.Batman.js , Angular.js 前端中的MVVM设计模式让UI与数据模型可以很轻松的相互更新,这意味着 ...
- struts 模型驱动
情景: 有一个用来处理用户的UserAction 类, 该动作类实现了 ModelDriven 接口. 当用户触发UserAction 动作时, ModelDriven 拦截器将调用相关UserAct ...
- java web环境配置类型问题
一, cmd查看jdk版本 java -version cmd查看jdk安装路径 java -verbose 二, 如果出现了上述的错误按照如下的3个步骤解决:1.首先关闭MyEclipse工作空间. ...
- 10.29_Extjs-lovcombo
(1) Ext.ux.form.LovCombo多选下拉框 :http://www.iteye.com/topic/340900 (2)combox:icon,lovcombo:icon (3) (4 ...
- struts2初印象
第一次写这么正式的文章,如果写的不好的地方,请指出. 今天玩了一下struts2,不过貌似是我被他玩了.简要笔记如下: 一.配置struts2(在eclipse Helios版本下) (1)先创建一个 ...
- linux - Mysql 创建用户和授权
CREATE USER 'cui'@'%' IDENTIFIED BY 'xxxxxxxxxxxxxxxxxx'; GRANT ALL ON test_db.* TO 'cui'@'%'; REVOK ...
- Linux中的堡垒--iptables
iptables的构成(四表五链) 表 filter:过滤数据包 nat :转换数据包的源或目标地址 mangle:用来mangle包,改变包的 ...
- Linux 经典面试题
[Linux 经典面试题] 1. 在Linux系统中,以 文件 方式访问设备 . 2. Linux内核引导时,从文件 /etc/fstab 中读取要加载的文件系统. 3. Linux文件系统中每个文 ...