Quantum Network Stream Evaluator 4 — Problem Statement & Solution Guide
Problem Description
Given a tree with nodes and edges, find the centroid of the tree using Heavy-Light Decomposition algorithm.
Examples
Input
Given a tree with nodes [1, 2, 3, 4, 5] and edges [(0, 1), (0, 2), (1, 3), (1, 4)], find the centroid of the tree using Heavy-Light Decomposition algorithm.
Output
The centroid of the tree is node 1 with a sum of subtree as 1 + 2 + 3 + 4 = 10
Explanation: Step-by-step: First, we find the centroid of the tree by calculating the sum of each subtree. Then, we apply the Heavy-Light Decomposition algorithm to find the node with the maximum sum of its subtree.
Input
Given a tree with nodes [10, 20, 30, 40, 50] and edges [(0, 1), (0, 2), (1, 3), (1, 4)], find the centroid of the tree using Heavy-Light Decomposition algorithm.
Output
The centroid of the tree is node 1 with a sum of subtree as 10 + 20 + 30 + 40 = 100
Explanation: Step-by-step: First, we find the centroid of the tree by calculating the sum of each subtree. Then, we apply the Heavy-Light Decomposition algorithm to find the node with the maximum sum of its subtree.
Constraints
- 1 <= N <= 2 * 10^5
- -10^9 <= arr[i] <= 10^9
- Time Complexity: O(N log N) or O(N log^2 N)
- Space Complexity: O(N)
Optimal Approach & Strategy
Use Heavy-Light Decomposition to process subproblems in O(N log N) time and O(N) auxiliary memory.
Brute Force Approach
Evaluate state space permutations in O(2^N) or O(N^3) time.
Verified Code Solutions
function solution(tree) {
let centroid = -1;
let maxSum = -1;
for (let i = 0; i < tree.length; i++) {
let sum = 0;
let stack = [i];
let visited = new Set();
while (stack.length > 0) {
let node = stack.pop();
if (!visited.has(node)) {
sum += tree[node];
visited.add(node);
for (let j = 0; j < tree.length; j++) {
if (tree[j] !== 0 && !visited.has(j)) {
stack.push(j);
}
}
}
}
if (sum > maxSum) {
maxSum = sum;
centroid = i;
}
}
return centroid;
}class Solution {
public:
int solution(vector<int>& tree) {
int centroid = -1;
int maxSum = -1;
for (int i = 0; i < tree.size(); i++) {
int sum = 0;
vector<bool> visited(tree.size(), false);
sum = dfs(tree, i, visited);
if (sum > maxSum) {
maxSum = sum;
centroid = i;
}
}
return centroid;
}
int dfs(vector<int>& tree, int node, vector<bool>& visited) {
if (visited[node]) return 0;
visited[node] = true;
int sum = tree[node];
for (int i = 0; i < tree.size(); i++) {
if (tree[i] != 0 && !visited[i]) {
sum += dfs(tree, i, visited);
}
}
return sum;
}
}class Solution {
public int solution(int[] tree) {
int centroid = -1;
int maxSum = -1;
for (int i = 0; i < tree.length; i++) {
int sum = 0;
boolean[] visited = new boolean[tree.length];
sum = dfs(tree, i, visited);
if (sum > maxSum) {
maxSum = sum;
centroid = i;
}
}
return centroid;
}
public int dfs(int[] tree, int node, boolean[] visited) {
if (visited[node]) return 0;
visited[node] = true;
int sum = tree[node];
for (int i = 0; i < tree.length; i++) {
if (tree[i] != 0 && !visited[i]) {
sum += dfs(tree, i, visited);
}
}
return sum;
}
}def solution(tree):
centroid = -1
max_sum = -1
for i in range(len(tree)):
sum = 0
stack = [i]
visited = set()
while stack:
node = stack.pop()
if node not in visited:
sum += tree[node]
visited.add(node)
for j in range(len(tree)):
if tree[j] != 0 and j not in visited:
stack.append(j)
if sum > max_sum:
max_sum = sum
centroid = i
return centroidfunction solution(tree) {
let centroid = -1;
let maxSum = -1;
for (let i = 0; i < tree.length; i++) {
let sum = 0;
let stack = [i];
let visited = new Set();
while (stack.length > 0) {
let node = stack.pop();
if (!visited.has(node)) {
sum += tree[node];
visited.add(node);
for (let j = 0; j < tree.length; j++) {
if (tree[j] !== 0 && !visited.has(j)) {
stack.push(j);
}
}
}
}
if (sum > maxSum) {
maxSum = sum;
centroid = i;
}
}
return centroid;
}Asked in Top Tech Interviews
Solve in Interative Editor
Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.