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的更多相关文章

  1. hackerrank答案

    regex: https://github.com/taiyang-li/hackerrank/tree/master/hackerrank/regex-exercise awk: https://g ...

  2. 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 ...

  3. HackerRank "Self Balancing Tree"

    Something to learn: Rotation ops in AVL tree does not require recursion. https://github.com/andreima ...

  4. *[hackerrank]Cut the tree

    https://www.hackerrank.com/contests/w2/challenges/cut-the-tree 树分成两部分,求两部分差最小.一开始想多了,后来其实求一下总和,求一下分部 ...

  5. 【HackerRank】Cut the tree

    题目链接:Cut the tree 题解:题目要求求一条边,去掉这条边后得到的两棵树的节点和差的绝对值最小. 暴力求解会超时. 如果我们可以求出以每个节点为根的子树的节点之和,那么当我们去掉一条边(a ...

  6. 【HackerRank】Utopian tree

    The Utopian tree goes through 2 cycles of growth every year. The first growth cycle of the tree occu ...

  7. HackerRank "Kundu and Tree" !!

    Learnt from here: http://www.cnblogs.com/lautsie/p/3798165.html Idea is: we union all pure black edg ...

  8. HackerRank "The Indian Job"

    A sly knapsack problem in disguise! Thanks to https://github.com/bhajunsingh/programming-challanges/ ...

  9. HackerRank "Array and simple queries" !

    The most interesting, flexible and juicy binary tree problem I have ever seen. I learnt it from here ...

随机推荐

  1. 20160409 javaweb 数据库连接池

    1.自己编写数据库连接池: package com.dzq.pool; import java.io.PrintWriter; import java.lang.reflect.InvocationH ...

  2. Android开发之ViewPager

    什么是ViewPager? ViewPager是安卓3.0之后提供的新特性,继承自ViewGroup,专门用以实现左右滑动切换View的效果. 如果想向下兼容就必须要android-support-v ...

  3. MVC小系列(二十一)【带扩展名的路由可能失效】

    mvc3之后:如果路由带上扩展名,比如这样: public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRout ...

  4. 安装完oracle重新启动后报ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务(重启前正常)

    安装完oracle重新启动后报ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务(重启前正常) 刚安装完后用plSql登录正常. 在dos命令行下 输入  sqlplus 用户 ...

  5. ssh连接远程linux服务器

    1.在百度搜索输入"putty"然后进行下载,下载后无需安装只需要在文件中找到"putty.exe"双击即可运行. 2.在"Host Name or ...

  6. 配置iSCSI

    先查下yum list | grep iscsi, 存在iscsi包, 进行安装:yum install iscsi-initiator-utils.x86_64, cat /etc/iscsi/in ...

  7. MateSublg

    MateSublg 说明 使用MetaWeblog的方式提交文章,并自动上传图片. 本插件的官方地址:MateSublg – Sollyu博客 本插件的开源地址:sollyu / MetaSubolg ...

  8. 【原创】Linux 内核模块编程

    sudo gedit hello.c #include <linux/module.h> #include <linux/kernel.h> #include <linu ...

  9. html5生成柱状图(条形图)

    <html> <canvas id="a_canvas" width="1000" height="700">< ...

  10. PL/SQL学习(二)条件和循环语句

      原文参考:http://plsql-tutorial.com/   PLSQL条件语句 IF THEN ELSE STATEMENT 1) IF condition THEN  statement ...