Cut the tree _ HackerRank

有意思的题目,但是题目描述不正确(貌似阿三哥出的题目),让我走了一些弯路,很tmd无语。。输入严格来说根本不是树,是图来的,因为边是无向边。。但是它的输入保证了是没有环路的图,所以某种程度上可以看做是树(任何一个节点都可以为根节点,然后递归地把每个节点的邻居看做子节点)。

不过话说回来如果一开始告诉我这个图,我还真想不出来解法。。但是因为它“误导”了我,让我认为它是一颗树后,问题就简单了,每个树节点递归地存储它子树的总和。然后深度遍历即可。。

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

	static int n;
	static int[] nodevals;
	static LinkedList<Integer>[] nodes;
	static int[] nodesum;
	static int root;
	static int minDif = Integer.MAX_VALUE;

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */

    	Scanner scan = new Scanner(System.in);
    	n = scan.nextInt();

    	nodevals = new int[n+1];
    	nodes = new LinkedList[n+1];
    	nodesum = new int[n+1];

    	for(int i = 1;i<=n;i++)
    	{
    		nodevals[i] = scan.nextInt();
    		nodes[i] = new LinkedList<Integer>();
    	}

    	for(int i = 1; i<n;i++)
    	{
    		int par = scan.nextInt();
    		int chd = scan.nextInt();

    		nodes[par].addFirst(chd);
    		nodes[chd].addFirst(par);
    	}

    	depthSum(1,-1);

    	depthMinDif(1,-1);

    	System.out.println(minDif);
    }

    static int depthSum(int nodeInd,int par)
    {
    	nodesum[nodeInd] += nodevals[nodeInd];

    	for(int chd : nodes[nodeInd]){

    		if(chd == par)
    			continue;

    		nodesum[nodeInd]+=depthSum(chd,nodeInd);
    	}
    	return nodesum[nodeInd] ;
    }

    static void depthMinDif(int nodeInd, int par){
    	int tmpdif = Math.abs(nodesum[1] - 2 * nodesum[nodeInd]);
    	if(tmpdif < minDif)
    		minDif = tmpdif;

    	for(int chd : nodes[nodeInd]){
    		if(chd == par)
    			continue;
    		depthMinDif(chd,nodeInd);
    	}
    }
}
时间: 2024-10-25 21:31:12

Cut the tree _ HackerRank的相关文章

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

Similar Pair _ HackerRank

Hacker rank真的比leetcode 难了不止一个等级.. 这题有点巧妙..深度搜索每条路径,然后枚举,基本很多人都想的出来,但是关键在于这样肯定超时.巧妙之处在于要给每条路径建立一个线段树来加速查询,每次similar查询复杂度从O(h)变成O(lgh).. 犯了两个错误 (1)要用long来存储线段树,以及可能的similar pairs. (2)值减去T可能小于0,值加上T也有可能大于n. import java.io.*; import java.util.*; import j

HackerRank &quot;The Indian Job&quot;

A sly knapsack problem in disguise! Thanks to https://github.com/bhajunsingh/programming-challanges/tree/master/hackerrank/algorithms/the-indian-jobLesson learnt: The Italian\Indian job is two-way 01 Knapsack. And some complext problem can be convert

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

BZOJ 2588 Count on a tree 主席树+倍增LCA

题目大意:给定一棵树,每个节点有权值,询问两个节点路径上的权值第k小 这题很卡时间... 树链剖分+二分+树套树的O(nlog^4n)做法可以去死了 没有修改操作,树链剖分+二分+划分树O(nlog^3n),还是死了 我怒了,裸学了一发可持久化线段树(不看任何代码OTZ,我是怎么做到的0.0),二分+主席树,O(nlog^2n),居然还是死了! 最后发现我SB了,完全没有必要二分,直接把4个参全传下去就行了,O(nlogn) 首先我们对于每个节点维护这个节点到根的权值线段树 然后对于每个询问(x

Tree Fold

package tree object Fold { def fold[A, B](t: Tree[A])(f: A => B)(g: (B, B) => B): B = t match { case Leaf(a) => f(a) case Branch(l, r) => g(fold(l)(f)(g), fold(r)(f)(g)) } def main(args: Array[String]): Unit = { val tree = Branch(Leaf(1), Bran

Tree MapByFold

package tree object MapByFold { def map[A, B](t: Tree[A])(f: A => B): Tree[B] = Fold.fold(t)(a => Leaf(f(a)): Tree[B])(Branch(_, _)) def main(args: Array[String]): Unit = { val tree = Branch(Leaf(1), Branch(Branch(Branch(Leaf(3), Branch(Leaf(5), Lea

Tree Map

package tree object Map { def map[A, B](t: Tree[A])(f: A => B): Tree[B] = t match { case Leaf(a) => Leaf(f(a)) case Branch(l, r) => Branch(map(l)(f), map(r)(f)) } def main(args: Array[String]): Unit = { val tree = Branch(Leaf(1), Branch(Branch(Br

泛函编程(8)-数据结构-Tree

上节介绍了泛函数据结构List及相关的泛函编程函数设计使用,还附带了少许多态类型(Polymorphic Type)及变形(Type Variance)的介绍.有关Polymorphism的详细介绍会放在typeclass讨论中.为了更多了解泛函数据结构(Functional Data Structure),想在这个章节把另一个我们熟悉的数据结构-Tree做些简单介绍. Tree的状态不是枝(Branch)就是叶(Leaf),这个很容易理解.那么就按照上节设计List那样设计Tree类型: 1