https://www.hackerrank.com/contests/w2/challenges/cut-the-tree

树分成两部分,求两部分差最小。一开始想多了,后来其实求一下总和,求一下分部的和就行了。

#include <cstdlib>
#include <climits>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std; vector<vector<int>> tree;
vector<bool> visited;
vector<int> val;
int totalVal; int subSum(int root, int& minCut) {
int sum = 0;
visited[root] = true;
for (int i = 0; i < tree[root].size(); i++) {
int sub = tree[root][i];
if (visited[sub])
continue;
int x = subSum(sub, minCut);
sum += x;
}
sum += val[root];
minCut = min(minCut, abs(totalVal - 2 * sum));
return sum;
}
int main() {
int N;
cin >> N;
tree.resize(N + 1);
visited.resize(N + 1);
val.resize(N + 1);
totalVal = 0;
for (int i = 1; i <= N; i++) {
cin >> val[i];
totalVal += val[i];
}
for (int i = 0; i < N - 1; i++) {
int a, b;
cin >> a >> b;
tree[a].push_back(b);
tree[b].push_back(a);
}
int minCut = INT_MAX;
// pick 1 as root
subSum(1, minCut);
cout << minCut << endl;
return 0;
}

  

*[hackerrank]Cut the tree的更多相关文章

  1. 【HackerRank】Cut the tree

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

  2. HackerRank "Self Balancing Tree"

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

  3. 【HackerRank】Utopian tree

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

  4. HackerRank "Kundu and Tree" !!

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

  5. the apple tree

    the apple tree A long time ago, there was a huge apple tree. A little boy loved to come and lay arou ...

  6. Factoextra R Package: Easy Multivariate Data Analyses and Elegant Visualization

    factoextra is an R package making easy to extract and visualize the output of exploratory multivaria ...

  7. knowledge, Experience & Creativity

    In a training session, the trainer asked the audience "knowledge is power, how many of you agre ...

  8. 【原创】大数据基础之Hive(5)性能调优Performance Tuning

    1 compress & mr hive默认的execution engine是mr hive> set hive.execution.engine;hive.execution.eng ...

  9. [游记] HEOI2018酱油记

    Day -1 在机房颓颓颓颓颓,晚上得知这次考试题本来是要给 ZJOI2018 用的,结果没用上..可想而知考试的难度.. 但愿不爆零 Day 0 坐了一上午火车,顺便找茁神犇拷了个 COD,然后接着 ...

随机推荐

  1. VS2015编译错误:调用的目标发生了异常--->此实现不是Windows平台FLPS验证的加密算法的一部分。

    在Win10下安装好几次VS2015(企业版)了,这次发生了一个奇怪的问题,错误截图如下: 控制台.WPF等项目均有此错误!但是ASP.NET项目却可以编译运行!一开始还以为VS2015安装错误,修复 ...

  2. 支持IE6的树形节结构TreeTable

    关于TreeTable实际应用的案例:http://www.cnblogs.com/qigege/p/5213689.html treeTable是跨浏览器.性能很高的jquery的树表组件,它使用非 ...

  3. 验证中文、英文、电话、手机、邮箱、数字、数字和字母、Url地址和Ip地址的正则表达式

    Helper类代码 public class Helper { #region 单列循环 private Helper() { } private static Helper instance = n ...

  4. [WinForm]TextBox只能输入数字或者正浮点型数字

    关键代码: /// <summary> /// 只能输入数字[KeyPress事件] /// </summary> /// <param name="textB ...

  5. logstash 统计告警

    在实际的项目中需要对线上日志做实时分析跟统计,这一套方案可以用现有的ELK(ElasticSearch, Logstash, Kibana)方案既可以满足,关于这个方案的具体的步骤可以参考网上的解决方 ...

  6. boost muti-thread

    背景 •       今天互联网应用服务程序普遍使用多线程来提高与多客户链接时的效率:为了达到最大的吞吐量,事务服务器在单独的线程上运行服务程序: GUI应用程序将那些费时,复杂的处理以线程的形式单独 ...

  7. Hashmap in java

    1. HashMap要点: 1.1 基本数据结构:  采用 数组+链表/平衡二叉查找树 的组合形式,所有键值对都以Entry<K,V>形式存储(每put进一个键值对,就会实例化一个Entr ...

  8. SQL SERVER 强制排序规则查询

    有时会需要在2个DB之间的数据做比较, 但因为一些原因, 数据库的默认排序规则是不一样的, 例如 SELECT A.Col1, B.Col1, A.* FROM DB1.dbo.A LEFT JOIN ...

  9. NodeJS - Express 4.0下使用app.dynamicHelpers错误

    在NodeJS - Express 4.0下使用app.dynamicHelpers发生错误: app.dynamicHelpers({ ^ TypeError: Object function (r ...

  10. android开发设置dialog的高宽

    这里设置为跟屏幕一样的宽度,:看代码 dlg.show(); WindowManager.LayoutParams params = dlg.getWindow().getAttributes(); ...