📌  相关文章
📜  给定 N-ary Tree 的每个顶点的最大子树和

📅  最后修改于: 2021-09-22 09:46:14             🧑  作者: Mango

给定一个由N 个节点组成的 N 数组树,以1 为根,边的形式为{u, v} ,以及一个由N 个整数组成的数组values[] 。每个顶点i都有一个由values[i]表示的整数值。任务是通过将每个顶点i的值添加到其子顶点的非空子集,找到每个顶点i可能的最大子树总和。

例子:

朴素的方法:最简单的方法是从1N遍历每个顶点i的子树,并对其进行DFS遍历。对于每个顶点i ,选择其具有非负值的子顶点的子集。如果所选顶点的子集为空,则搜索并打印具有最小值的节点,使其成为顶点i的子节点。否则,打印子集中存在的节点的节点值的总和。

时间复杂度: O(N 2 )
辅助空间: O(N)

高效的方法:想法是使用 DFS 遍历和动态编程方法。请按照以下步骤解决问题:

  • 初始化大小为N的数组ans[]以存储每个顶点的最大子树总和。
  • 对每个顶点执行 DFS 遍历,并为每个顶点初始化vans[v]具有一些大的负值。
  • 如果顶点v是叶顶点,则该顶点的答案将是values[v] 。因此,分配ans[v] = values[v]
  • 否则,遍历与顶点v相邻的顶点,对于每个相邻顶点u ,将ans[v]更新为ans[v] = max(ans[u] + values[v], values[v], ans[u])
  • 完成上述步骤后,打印ans[]数组中存储的值作为每个顶点的答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
#define V 3
#define M 2
 
// Function to perform the DFS
// Traversal on the given Tree
void dfs(int v, int p,
         vector adj[],
     int ans[], int vals[])
{
     
    // To check if v is leaf vertex
    bool isLeaf = 1;
 
    // Initialize answer for vertex v
    ans[v] = INT_MIN;
 
    // Traverse adjacency list of v
    for(int u : adj[v])
    {
        if (u == p)
            continue;
             
        isLeaf = 0;
        dfs(u, v, adj, ans, vals);
 
        // Update maximum subtree sum
        ans[v] = max(ans[u] + vals[v],
                 max(ans[u], vals[u]));
    }
 
    // If v is leaf
    if (isLeaf)
    {
        ans[v] = vals[v];
    }
}
 
// Function to calculate maximum
// subtree sum for each vertex
void printAnswer(int n,
                 int edges[V][M],
                 int values[])
{
     
    // Stores the adjacency list
    vector adj[n];
     
    // Add Edegs to the list
    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);
    }
 
    // Stores largest subtree
    // sum for each vertex
    int ans[n] ;
 
    // Calculate answer
    dfs(0, -1, adj, ans, values);
 
    // Print the result
    for(auto x : ans)
    {
        cout << x << " ";
    }
}
 
// Driver Code
int main()
{
     
    // Given nodes
    int N = 4;
 
    // Give N edges
    int edges[V][M] = { { 1, 2 },
                        { 1, 3 },
                        { 3, 4 } };
 
    // Given values
    int values[] = { 1, -1, 0, 1 };
 
    // Function Call
    printAnswer(N, edges, values);
}
 
// This code is contributed by Princi Singh


Java
// Java program for the above approach
 
import java.io.*;
import java.util.ArrayList;
 
@SuppressWarnings("unchecked")
class GFG {
 
    // Function to perform the DFS
    // Traversal on the given Tree
    static void dfs(int v, int p,
                    ArrayList adj[],
                    int ans[], int vals[])
    {
 
        // To check if v is leaf vertex
        boolean isLeaf = true;
 
        // Initialize answer for vertex v
        ans[v] = Integer.MIN_VALUE;
 
        // Traverse adjacency list of v
        for (int u : adj[v]) {
            if (u == p)
                continue;
            isLeaf = false;
            dfs(u, v, adj, ans, vals);
 
            // Update maximum subtree sum
            ans[v] = Math.max(
                ans[u] + vals[v],
                Math.max(ans[u],
                         vals[u]));
        }
 
        // If v is leaf
        if (isLeaf) {
            ans[v] = vals[v];
        }
    }
 
    // Function to calculate maximum
    // subtree sum for each vertex
    static void printAnswer(
        int n, int edges[][], int values[])
    {
 
        // Stores the adjacency list
        ArrayList adj[]
            = new ArrayList[n];
 
        for (int i = 0; i < n; i++)
            adj[i] = new ArrayList<>();
 
        // Add Edegs to the list
        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);
        }
 
        // Stores largest subtree
        // sum for each vertex
        int ans[] = new int[n];
 
        // Calculate answer
        dfs(0, -1, adj, ans, values);
 
        // Print the result
        for (int x : ans) {
            System.out.print(x + " ");
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given nodes
        int N = 4;
 
        // Give N edges
        int edges[][]
            = new int[][] { { 1, 2 },
                            { 1, 3 },
                            { 3, 4 } };
 
        // Given values
        int values[] = { 1, -1, 0, 1 };
 
        // Function Call
        printAnswer(N, edges, values);
    }
}


Python3
# Python3 program for the above approach
 
V = 3
M = 2
 
# Function to perform the DFS
# Traversal on the given Tree
def dfs(v, p):
 
    # To check if v is leaf vertex
    isLeaf = 1
 
    # Initialize answer for vertex v
    ans[v] = -10**9
 
    # Traverse adjacency list of v
    for u in adj[v]:
        if (u == p):
            continue
 
        isLeaf = 0
        dfs(u, v)
 
        # Update maximum subtree sum
        ans[v] = max(ans[u] + vals[v],max(ans[u], vals[u]))
 
    # If v is leaf
    if (isLeaf):
        ans[v] = vals[v]
 
# Function to calculate maximum
# subtree sum for each vertex
def printAnswer(n, edges, vals):
 
    # Stores the adjacency list
    # vector adj[n];
 
    # Add Edegs to the list
    for i in range(n - 1):
        u = edges[i][0] - 1
        v = edges[i][1] - 1
 
        adj[u].append(v)
        adj[v].append(u)
 
    # Calculate answer
    dfs(0, -1)
 
    # Prthe result
    for x in ans:
        print(x, end=" ")
 
# Driver Code
if __name__ == '__main__':
 
    # Given nodes
    N = 4
 
    # Give N edges
    edges=[ [ 1, 2],
            [ 1, 3],
            [ 3, 4] ]
 
    adj=[[] for i in range(N)]
    ans=[0 for i in range(N)]
 
    # Given values
    vals=[1, -1, 0, 1]
 
    # Function Call
    printAnswer(N, edges, vals)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the
// above approach
using System;
using System.Collections.Generic;
class GFG{
 
// Function to perform the DFS
// Traversal on the given Tree
static void dfs(int v, int p,
                List []adj,
                int []ans, int []vals)
{
  // To check if v is leaf
  // vertex
  bool isLeaf = true;
 
  // Initialize answer for
  // vertex v
  ans[v] = int.MinValue;
 
  // Traverse adjacency list
  // of v
  foreach (int u in adj[v])
  {
    if (u == p)
      continue;
    isLeaf = false;
    dfs(u, v, adj, ans, vals);
 
    // Update maximum subtree
    // sum
    ans[v] = Math.Max(ans[u] +
                      vals[v],
             Math.Max(ans[u],
                      vals[u]));
  }
 
  // If v is leaf
  if (isLeaf)
  {
    ans[v] = vals[v];
  }
}
 
// Function to calculate maximum
// subtree sum for each vertex
static void printAnswer(int n,
                        int [,]edges,
                        int []values)
{
  // Stores the adjacency list
  List []adj =
       new List[n];
 
  for (int i = 0; i < n; i++)
    adj[i] = new List();
 
  // Add Edegs to the list
  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);
  }
 
  // Stores largest subtree
  // sum for each vertex
  int []ans = new int[n];
 
  // Calculate answer
  dfs(0, -1, adj,
      ans, values);
 
  // Print the result
  foreach (int x in ans)
  {
    Console.Write(x + " ");
  }
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given nodes
  int N = 4;
 
  // Give N edges
  int [,]edges = new int[,] {{1, 2},
                             {1, 3},
                             {3, 4}};
  // Given values
  int []values = {1, -1, 0, 1};
 
  // Function Call
  printAnswer(N, edges, values);
}
}
 
// This code is contributed by gauravrajput1


Javascript


输出:
2 -1 1 1

时间复杂度: O(N)
辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程