HackerRank - Cut the Tree

Typical tree recursion works. The key is: ANY node can be treated as the root, since it is about edge, not about node. This simplifies things a lot.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <unordered_set>
using namespace std;

typedef long long long64;

struct Node
{
    Node() :val(0){}
    Node(unsigned v) : val(v){}
    long64 val;
    unordered_set<int> inx;
};

long64 ret = std::numeric_limits<unsigned>::max();

long64 go(vector<Node> &nodes, int root, long64 tsum)
{
    Node &n = nodes[root];

    long64 currsum = n.val;
    for (int i : n.inx)
    {
        nodes[i].inx.erase(root);
        currsum += go(nodes, i, tsum);
    }

    ret = std::min(std::abs(tsum - 2 * currsum), ret);

    return currsum;
}

int main()
{
    int n; cin >> n;
    vector<Node> nodes(n);

    long64 sum = 0;
    for (int i = 0; i < n; i++)
    {
        unsigned v; cin >> v;
        sum += v;
        nodes[i].val = v;
    }

    for (int i = 0; i < n - 1; i++)
    {
        int pi, ci; cin >> pi >> ci;
        nodes[pi - 1].inx.insert(ci - 1);
        nodes[ci - 1].inx.insert(pi - 1);
    }

    go(nodes, 0, sum);
    cout << ret << endl;

    return 0;
}
时间: 2024-10-04 01:45:21

HackerRank - Cut the Tree的相关文章

【HackerRank】Utopian tree

The Utopian tree goes through 2 cycles of growth every year. The first growth cycle of the tree occurs during the monsoon, when it doubles in height. The second growth cycle of the tree occurs during the summer, when its height increases by 1 meter.

Cut the tree _ HackerRank

有意思的题目,但是题目描述不正确(貌似阿三哥出的题目),让我走了一些弯路,很tmd无语..输入严格来说根本不是树,是图来的,因为边是无向边..但是它的输入保证了是没有环路的图,所以某种程度上可以看做是树(任何一个节点都可以为根节点,然后递归地把每个节点的邻居看做子节点). 不过话说回来如果一开始告诉我这个图,我还真想不出来解法..但是因为它"误导"了我,让我认为它是一颗树后,问题就简单了,每个树节点递归地存储它子树的总和.然后深度遍历即可.. import java.io.*; imp

HackerRank &quot;Self Balancing Tree&quot;

Something to learn: Rotation ops in AVL tree does not require recursion. https://github.com/andreimaximov/hacker-rank/blob/master/data-structures/tree/self-balancing-tree/main.cpp node *create_node(int val) { node *pNew = new node; pNew->val = val; p

the apple tree

the apple tree A long time ago, there was a huge apple tree. A little boy loved to come and lay around it every day. He climbed to the tree top, ate -the apples, took a nap under the shadow... He loved the tree and the tree loved to play with him. Ti

codechef May Challenge 2016 FORESTGA: Forest Gathering 二分

Description All submissions for this problem are available. Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. Chef is the head of commercial logging industry that recently bought a farm containing N trees. You are given in

区间内x的出现个数(主席树)

题目大概:求区间内x出现的次数 出题人yjy Description ZJK 给你一个长度为 n 的数列和 m 次询问,每次询问从第 l 个到第 r 个数中,数 x 出现了多少次.Input第一行一个整数 n,第二行 n 个整数,表示这个数列.第三行一个整数 m,表示询问数.下面 m 行,每行三个整数 l, r, x,表示询问[l, r]之间数 x 出现的次数Output对于每个询问操作,输出该询问的答案.答案之间用换行隔开,一共 m 行.Example61 1 2 3 3 181 6 13 5

Codeforces Round #339 (Div. 2) A. Link/Cut Tree

A. Link/Cut Tree Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, which is based on Splay trees. Specifically, he is now studying the expose procedure. Unfortunately, Rostislav is unable to understand the definition

AC日记——【模板】Link Cut Tree 洛谷 P3690

[模板]Link Cut Tree 思路: LCT模板: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 300005 int n,m,val[maxn]; int top,ch[maxn][2],f[maxn],xr[maxn],q[maxn],rev[maxn]; inline void in(int &now) { int if_z=1;now=0; char Cget=getchar(); while

bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门

link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isroot(int x):判断x是否为所在重链(splay)的根 void down(int x):下放各种标记 void rotate(int x):在x所在重链(splay)中将x旋转到fa[x]的位置上 void splay(int x):在x坐在重链(splay)中将x旋转到根 void acce