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的更多相关文章
-
【HackerRank】Cut the tree
题目链接:Cut the tree 题解:题目要求求一条边,去掉这条边后得到的两棵树的节点和差的绝对值最小. 暴力求解会超时. 如果我们可以求出以每个节点为根的子树的节点之和,那么当我们去掉一条边(a ...
-
HackerRank ";Self Balancing Tree";
Something to learn: Rotation ops in AVL tree does not require recursion. https://github.com/andreima ...
-
【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 ...
-
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 ...
-
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 ...
-
knowledge, Experience &; Creativity
In a training session, the trainer asked the audience "knowledge is power, how many of you agre ...
-
【原创】大数据基础之Hive(5)性能调优Performance Tuning
1 compress & mr hive默认的execution engine是mr hive> set hive.execution.engine;hive.execution.eng ...
-
[游记] HEOI2018酱油记
Day -1 在机房颓颓颓颓颓,晚上得知这次考试题本来是要给 ZJOI2018 用的,结果没用上..可想而知考试的难度.. 但愿不爆零 Day 0 坐了一上午火车,顺便找茁神犇拷了个 COD,然后接着 ...
随机推荐
-
MVVM架构~knockoutjs系列之验证成功提示显示
返回目录 对于knockout.validation来说,我们已经知道了如何去验证大部分表单元素,而有时,我们的需求希望在每个元素验证成功后,去显示正确的提示,这个我们很容易的使用self.元素.is ...
-
15款优秀移动APP产品原型设计工具
一新来小盆友问:“移动产品原型设计都用啥工具?” 答:“@#¥……&%*” 又问:“能详细说下各个工具吗?我比较一下” “……” 好吧,谁让我那么的爱分享而你又是小美女呢 ———————正文开 ...
-
PCI Express(三) - A story of packets, stack and network
原文出处:http://www.fpga4fun.com/PCI-Express3.html Packetized transactions PCI express is a serial bus. ...
-
POJ 1703 Find them, Catch them(种类并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 41463 Accepted: ...
-
Git的优势
分布式,强调个体 公共服务器压力和数据量都不会太大 速度快.灵活 任意两个开发者之间可以很容易的解决冲突 离线工作
-
js实用功能
//日期格式转换 Date.prototype.format = function (format) { /* * eg:format="yyyy-MM-dd hh:mm: ...
-
Ajax调用返回json,xml数据类型(0517--pm)
一.返回Json型数据: 1.主页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...
-
Jasper_sheetName_defined by parameter or hard coding or filed name
1.根据传递的参数定义sheet name (jasper sheet name defined by parameter) (1) 获取后台参数 <parameter name="P ...
-
jquery ajax协调SpringMVCD实现局部刷新IV
feedback.jsp: <%@ page language="java" import="java.util.*" pageEncoding=&quo ...
-
51Nod 1182 完美字符串(字符串处理 贪心 Facebook Hacker Cup选拔)
1182 完美字符串 题目来源: Facebook Hacker Cup选拔 基准时间限制:1 秒 空间限制:1 ...