*[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 ...
随机推荐
- skynet启动过程_1
skynet的启动时需带个配置文件,这个文件其实是作为lua全局变量用的,见 int main(int argc, char *argv[]) { const char * config_file = ...
- JavaScript入门(5)
一.什么是数组? 数组是一个值的集合,每一个值都有一个索引号,从0开始,每个索引都有一个相应的值,根据需要添加更多数值. 好比一个团,团里有很多人.如下使用数组存储5个学生成绩: 二.如何创建数组 使 ...
- react 资源汇总
前端变化虽快,但其实一直都围绕这几个概念在转: URL - 访问什么页面 Data - 显示什么信息 View - 页面长成什么样 Action - 对页面做了什么操作 API Server - Da ...
- Asp.net自制模板框架
最近在做个包含了大量重复样式的控件的网站. 如果用MVC的话,我可以针对每个需要单独块编辑的Model写一个局部视图. 然后根据不同逻辑需求拼起来.用现成MVC框架非常容易做到. 但环境是3.5的最高 ...
- SQL Server 阻止了对组件 'xp_cmdshell' 的 过程'sys.xp_cmdshell' 的访问
sql server 2005: EXEC sp_configure N'show advanced options', N'1' RECONFIGURE WITH OVERRIDEEXEC sp_c ...
- HTML标签_Form
理解HTML是如何跳转到java程序中去的:Form常用HTML标签的作用<body> <form action="servlet/UploadServlet" ...
- 关于GC进行垃圾回收的时机
前言 今天查看一个同事的代码,发现代码中多处地方使用了GC.Collect()方法,我问他为什么这么做,他说感觉程序中定义了好多变量,怕GC回收不及时,用GC.Collect()可以手动掌控GC进行垃 ...
- Block中的引用循环
原文地址:http://www.cnblogs.com/lujianwenance/p/5910490.html Block在实际的开发中非常的常用,事件回调.传值.封装成代码块调用等等.很多人都对b ...
- Object-C在Nil上调用方法
在Object-C中,nil对象的作用等同于很多其它语言的NULL指针.不同的地方在于,在nil上调用方法不会导致程序崩溃或抛出异常. 这种技术被用在很多地方,但是对于我们来讲,最主要的就是我们不用在 ...
- iOS svn版本回退 cornerstone
http://blog.csdn.net/x32sky/article/details/46866899 IOS开发中,SVN如何恢复到某一个版本(以Cornerstone为例) Cornerst ...