/******************************************************* * Copyright (C) 2015 Haotian Wu * * This file is the solution to the question: * https://www.hackerrank.com/challenges/cut-the-tree * * Redistribution and use in source and binary forms are permitted. *******************************************************/ #include #include #include #include #include #include #include #include #include using namespace std; // Obviously, we cannot compute the sum of both subtrees on the fly while we traverse all the edges. // This problem can be solved using a search algorithm. We use recursive search here. // The main observation is that, we can keep track of the sum of one subtree, and the sum of all nodes, so the sum of the other subtree can be computed. int n; int w[100001]; vector adj[100001]; int visited[100001]; int ans = 200000000; int sum = 0; int search(int root) { visited[root]=1; int len = adj[root].size(); int sumofthistree = w[root]; for (int i=0;i