Given an N-array tree of N nodes, rooted at 1, with edges in the form {u, v}, and an array values[] consisting of N integers. Each vertex i has an integer value denoted by values[i]. The task is to find the largest Subtree Sum possible for each vertex i by adding its value to a non-empty subset of its child vertices.
Examples:
Input: Edges[][] = {{1, 2}, {1, 3}, {3, 4}}, values[] = {1, -1, 0, 1}
Output: 2 -1 1 1
Explanation:
Below is the given Tree:
1
/ \
2 3
\
4
Following subsets can be chosen for each vertex:
Vertex 1: Subset of vertices {1, 3, 4} can be chosen with values {1, 0, 1}. Therefore, sum = 1 + 0 + 1 = 2.
Vertex 2: Subset of vertices {2} can be chosen with values {-1}. Therefore, sum = -1.
Vertex 3: Subset of vertices {3, 4} can be chosen with values {0, 1}. Therefore, sum = 0 + 1 = 1.
Vertex 4: Subset of vertices {4} can be chosen with values {1}. Therefore, sum = 1.
Input: Edges[][] = {{1, 2}, {1, 3}, {2, 4}, {2, 5}}, values[] = {1, -1, -2, 1, 3}
Output: 5 4 -2 1 3
Explanation:
Below is the given Tree:
1
/ \
2 3
/ \
4 5
Following subsets can be chosen for each vertex:
Vertex 1: Subset of vertices {1, 4, 5} can be chosen with values {1, 1, 3}. Therefore, sum = 1 + 1 + 3 = 5.
Vertex 2: Subset of vertices {4, 5} can be chosen with values {1, 3}. Therefore, sum = 1 + 3 = 4.
Vertex 3: Subset of vertices {3} can be chosen with values {-2}. Therefore, sum = -2.
Vertex 4: Subset of vertices {4} can be chosen with values {1}. Therefore, sum = 1.
Vertex 5: Subset of vertices {5} can be chosen with values {3}. Therefore, sum = 3.
Naive Approach: The simplest approach is to traverse the subtree of each vertex i from 1 to N and perform DFS Traversal on it. For each vertex i, choose the subset of its child vertices having non-negative values. If the subset of chosen vertices is empty, then search and print the node having the minimum value such that it is the child node of vertex i. Otherwise, print the sum of node values of nodes present in the subset.
Time Complexity: O(N2)
Auxiliary Space: O(N)
Efficient Approach: The idea is to use DFS Traversal and Dynamic programming approach. Follow the below steps to solve the problem:
- Initialize an array ans[] of size N to store the maximum subtree sum for each vertex.
- Perform DFS Traversal for each vertex and for each vertex initialize v, ans[v] with some large negative value.
- If vertex v is a leaf vertex, then the answer for that vertex would be values[v]. Therefore, assign ans[v] = values[v].
- Otherwise, traverse the vertices adjacent to vertex v and for each adjacent vertex u, update ans[v] as ans[v] = max(ans[u] + values[v], values[v], ans[u]).
- After the above steps, print the values stored in ans[] array as the answer for each vertex.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define V 3
#define M 2
void dfs( int v, int p,
vector< int > adj[],
int ans[], int vals[])
{
bool isLeaf = 1;
ans[v] = INT_MIN;
for ( int u : adj[v])
{
if (u == p)
continue ;
isLeaf = 0;
dfs(u, v, adj, ans, vals);
ans[v] = max(ans[u] + vals[v],
max(ans[u], vals[u]));
}
if (isLeaf)
{
ans[v] = vals[v];
}
}
void printAnswer( int n,
int edges[V][M],
int values[])
{
vector< int > adj[n];
for ( int i = 0; i < n - 1; i++)
{
int u = edges[i][0] - 1;
int v = edges[i][1] - 1;
adj[u].push_back(v);
adj[v].push_back(u);
}
int ans[n] ;
dfs(0, -1, adj, ans, values);
for ( auto x : ans)
{
cout << x << " " ;
}
}
int main()
{
int N = 4;
int edges[V][M] = { { 1, 2 },
{ 1, 3 },
{ 3, 4 } };
int values[] = { 1, -1, 0, 1 };
printAnswer(N, edges, values);
}
|
Java
import java.io.*;
import java.util.ArrayList;
@SuppressWarnings ( "unchecked" )
class GFG {
static void dfs( int v, int p,
ArrayList<Integer> adj[],
int ans[], int vals[])
{
boolean isLeaf = true ;
ans[v] = Integer.MIN_VALUE;
for ( int u : adj[v]) {
if (u == p)
continue ;
isLeaf = false ;
dfs(u, v, adj, ans, vals);
ans[v] = Math.max(
ans[u] + vals[v],
Math.max(ans[u],
vals[u]));
}
if (isLeaf) {
ans[v] = vals[v];
}
}
static void printAnswer(
int n, int edges[][], int values[])
{
ArrayList<Integer> adj[]
= new ArrayList[n];
for ( int i = 0 ; i < n; i++)
adj[i] = new ArrayList<>();
for ( int i = 0 ; i < n - 1 ; i++) {
int u = edges[i][ 0 ] - 1 ;
int v = edges[i][ 1 ] - 1 ;
adj[u].add(v);
adj[v].add(u);
}
int ans[] = new int [n];
dfs( 0 , - 1 , adj, ans, values);
for ( int x : ans) {
System.out.print(x + " " );
}
}
public static void main(String[] args)
{
int N = 4 ;
int edges[][]
= new int [][] { { 1 , 2 },
{ 1 , 3 },
{ 3 , 4 } };
int values[] = { 1 , - 1 , 0 , 1 };
printAnswer(N, edges, values);
}
}
|
Python3
V = 3
M = 2
def dfs(v, p):
isLeaf = 1
ans[v] = - 10 * * 9
for u in adj[v]:
if (u = = p):
continue
isLeaf = 0
dfs(u, v)
ans[v] = max (ans[u] + vals[v], max (ans[u], vals[u]))
if (isLeaf):
ans[v] = vals[v]
def printAnswer(n, edges, vals):
for i in range (n - 1 ):
u = edges[i][ 0 ] - 1
v = edges[i][ 1 ] - 1
adj[u].append(v)
adj[v].append(u)
dfs( 0 , - 1 )
for x in ans:
print (x, end = " " )
if __name__ = = '__main__' :
N = 4
edges = [ [ 1 , 2 ],
[ 1 , 3 ],
[ 3 , 4 ] ]
adj = [[] for i in range (N)]
ans = [ 0 for i in range (N)]
vals = [ 1 , - 1 , 0 , 1 ]
printAnswer(N, edges, vals)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void dfs( int v, int p,
List< int > []adj,
int []ans, int []vals)
{
bool isLeaf = true ;
ans[v] = int .MinValue;
foreach ( int u in adj[v])
{
if (u == p)
continue ;
isLeaf = false ;
dfs(u, v, adj, ans, vals);
ans[v] = Math.Max(ans[u] +
vals[v],
Math.Max(ans[u],
vals[u]));
}
if (isLeaf)
{
ans[v] = vals[v];
}
}
static void printAnswer( int n,
int [,]edges,
int []values)
{
List< int > []adj =
new List< int >[n];
for ( int i = 0; i < n; i++)
adj[i] = new List< int >();
for ( int i = 0;
i < n - 1; i++)
{
int u = edges[i, 0] - 1;
int v = edges[i, 1] - 1;
adj[u].Add(v);
adj[v].Add(u);
}
int []ans = new int [n];
dfs(0, -1, adj,
ans, values);
foreach ( int x in ans)
{
Console.Write(x + " " );
}
}
public static void Main(String[] args)
{
int N = 4;
int [,]edges = new int [,] {{1, 2},
{1, 3},
{3, 4}};
int []values = {1, -1, 0, 1};
printAnswer(N, edges, values);
}
}
|
Javascript
<script>
function dfs(v, p, adj, ans, vals)
{
let isLeaf = true ;
ans[v] = Number.MIN_VALUE;
for (let u = 0; u < adj[v].length; u++)
{
if (adj[v][u] == p)
continue ;
isLeaf = false ;
dfs(adj[v][u], v, adj, ans, vals);
ans[v] = Math.max(ans[adj[v][u]] +
vals[v],
Math.max(ans[adj[v][u]],
vals[adj[v][u]]));
}
if (isLeaf)
{
ans[v] = vals[v];
}
}
function printAnswer(n, edges, values)
{
let adj = new Array(n);
for (let i = 0; i < n; i++)
adj[i] = [];
for (let i = 0; i < n - 1; i++)
{
let u = edges[i][0] - 1;
let v = edges[i][1] - 1;
adj[u].push(v);
adj[v].push(u);
}
let ans = new Array(n);
dfs(0, -1, adj, ans, values);
for (let x = 0; x < ans.length; x++)
{
document.write(ans[x] + " " );
}
}
let N = 4;
let edges = [[1, 2], [1, 3], [3, 4]];
let values = [1, -1, 0, 1];
printAnswer(N, edges, values);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)