*[hackerrank]Cut the tree
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,然后接着 ...
随机推荐
- IIS目录下文件共享后System.IO.File.Exists返回false
场景:在iis目录下,因为特殊需要共享一个文件夹,给到其他的技术人员访问,突然发现小小的操作,搞“大”了,使用 string path = Server.MapPath("~/file/te ...
- MongoDB五种树形结构表示法
MongoDB五种树形结构表示法 第一种:父链接结构 db.categories.insert( { _id: "MongoDB", parent: "Databases ...
- jquery 在ie10中post数据,最终数据丢失的BUG修复
最近在做项目的时候,发现ie10或者360之类套壳的浏览器(ie10) 在jquery调用post数据的时候,真实的请求并没有上传数据,原因不表,请见 http://stackoverflow.com ...
- Nginx+uWSGI+Django原理
Python的Web开发中,如果使用Django框架,那么较为成熟稳定的服务器架构一般是Nginx+uWSGI+Django.而为什么一定要三个结合在一起呢?直接使用Django的runserver来 ...
- stringlist
#ifndef _STRINGLIST_HPP_#define _STRINGLIST_HPP_ #include "../global.hpp"#include <type ...
- 高级php面试题及部分答案
在网上看到一些高级php 的面试题目.. 闲来无事,搞了一些答案...可能不是很全面,留这以后备用吧. 一. 基本知识点1.1 HTTP协议中几个状态码的含义:503 500 401 403 404 ...
- github pages
http://zyip.github.io/facemaker/index echo "hello world" >>hello.htm git init git ad ...
- wap开发使用jquery mobile之后页面不加载外部css样式文件/js文件
场景: wap开发,使用jquery mobile之后不会加载外部自定义的css文件了,需要手动刷新才会加载,查看外部自定义的js文件也是一样. 解决办法: 1.在page下面添加css样式,就不要写 ...
- HTML+CSS入门
<strong>加粗</strong> <em>斜体</em> <p>段落</p> <span>设置单独样式< ...
- [algothrim]URL相似度计算的思考
http://www.spongeliu.com/399.html http://in.sdo.com/?p=865