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. rinetd 安装使用

    1 下载解压: wget http://www.boutell.com/rinetd/http/rinetd.tar.gz tar zxvf rinetd.tar.gz 2 手动建立目录 mkdir ...

  2. JDK 动态代理实现原理

    一.引言 Java动态代理机制的出现,使得Java开发人员不用手工编写代理类,只要简单地指定一组接口及委托类对象便能动态生成代理类.代理类会负责将所有方法的调用分派到委托对象上反射执行,在分派执行的过 ...

  3. Spark菜鸟学习营Day4 单元测试程序的编写

    Spark菜鸟学习营Day4 单元测试程序的编写 Spark相比于传统代码是比较难以调试的,单元测试的编写是非常必要的. Step0:需求分析 在测试案例编写前,需完成需求分析工作,明确程序所有的输入 ...

  4. 22 高级SQL特性

    1.约束 为正确地进行关系数据库设计,需要一种方法来保证只在表中插入合法的数据.例如,如果Orders表存储订单信息,OrderItems表存储订单详细内容,应该保证Orderitems中引用的任何订 ...

  5. AVG()和to_date()函数的使用

    1.一道关于AVG函数和NULL值的面试题 某部门有10个员工,其中9个工资为1000,另一个为NULL,当使用AVG函数取该部门平均工资时,结果应该是多少? A.1000 B.900 我这里把题目再 ...

  6. JavaScript 代码片段

    1.无题 if (i && i.charAt(i.length - 1) == "/") { i = i.substr(0, i.length - 1) } 2.无 ...

  7. Mininet VM设置笔记

    Mininet VM是为了加快Mininet安装,而且可以很容易在linux平台上运行. VM运行在Windows,Mac,Linux,通过VMware.VirtualBox,QEMU和KVM. 下载 ...

  8. IIS WMI Provider

    section contains information about the classes that are implemented by the IIS WMI provider in the M ...

  9. ASP.NET Web – 状态管理

    状态类型 客户端或服务器资源 有效时间 ViewState 客户端 只在一个页面中 Cookie 客户端 关闭浏览器时会删除临时cookie,永久cookie存储在客户系统的磁盘上 Session 服 ...

  10. linux 删除某种规则命名的文件

    由于android开发需要删除以IMG_开头命名的图片文件,因此用到此命令 命令格式: rm IMG_*