📜  在N元树中计算具有给定总和的唯一路径

📅  最后修改于: 2021-04-27 23:54:21             🧑  作者: Mango

给定一个整数X和整数N ,任务是找到从N元树的根开始的唯一路径数,以使所有这些路径的总和等于X。 N -ary树满足以下条件:

  • 所有节点都有N个子节点,每个边的权重是不同的,并且在[1,N]范围内。
  • 这棵树一直延伸到无穷远。

例子:

天真的方法:最简单的方法是递归地找到所有可能的方法以获得等于X的路径总和并打印获得的计数。

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

高效方法:为了优化上述方法,其思想是使用动态编程。按照以下步骤解决问题:

  • 初始化一个dp []数组,该数组对于每个i索引,存储总计为i的路径计数。
  • 对于每个顶点,将strong> 1迭代为min(X,N),即其子级的所有可能值,并从所考虑的每个节点中以给定的总和找到可能的路径数。
  • 将使用边1N的所有路径相加,并检查计数是否已经计算。如果已经计算过,则返回该值。否则,通过考虑从当前顶点扩展树的所有可能方式,递归计算总和等于当前值的路径数。
  • 更新dp []数组并返回获得的计数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
#define ll long long
using namespace std;
const int mod = (int)1e9 + 7;
 
// Function for counting total
// no of paths possible with
// the sum is equal to X
ll findTotalPath(int X, int n,
                 vector& dp)
{
 
    // If the path of the sum
    // from the root to current
    // node is stored in sum
    if (X == 0) {
        return 1;
    }
    ll ans = 0;
 
    // If already commputed
    if (dp[X] != -1) {
        return dp[X];
    }
 
    // Count different no of paths
    // using all possible ways
    for (int i = 1; i <= min(X, n); ++i) {
 
        ans += findTotalPath(
                   X - i, n, dp)
               % mod;
        ans %= mod;
    }
 
    // Return total no of paths
    return dp[X] = ans;
}
 
// Driver Code
int main()
{
 
    int n = 3, X = 2;
 
    // Stores the number of ways
    // to obtains sums 0 to X
    vector dp(X + 1, -1);
 
    // Function call
    cout << findTotalPath(X, n, dp);
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG{
 
static int mod = (int)1e9 + 7;
 
// Function for counting total
// no of paths possible with
// the sum is equal to X
static int findTotalPath(int X, int n,
                         ArrayList dp)
{
     
    // If the path of the sum
    // from the root to current
    // node is stored in sum
    if (X == 0)
    {
        return 1;
    }
    int ans = 0;
 
    // If already commputed
    if (dp.get(X) != -1)
    {
        return dp.get(X);
    }
 
    // Count different no of paths
    // using all possible ways
    for(int i = 1; i <= Math.min(X, n); ++i)
    {
        ans += findTotalPath(X - i, n, dp) % mod;
        ans %= mod;
    }
 
    // Return total no of paths
    dp.set(X, ans);
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 3, X = 2;
     
    // Stores the number of ways
    // to obtains sums 0 to X
    ArrayList dp = new ArrayList(
        Collections.nCopies(X + 1, -1));
 
    // Function call
    System.out.print(findTotalPath(X, n, dp));
}
}
 
// This code is contributed by akhilsaini


Python3
# Python3 program for the above approach
mod = int(1e9 + 7)
 
# Function for counting total
# no of paths possible with
# the sum is equal to X
def findTotalPath(X, n, dp):
     
  # If the path of the sum
  # from the root to current
  # node is stored in sum
  if (X == 0):
    return 1
     
  ans = 0
 
  # If already commputed
  if (dp[X] != -1):
    return dp[X]
     
  # Count different no of paths
  # using all possible ways
  for i in range(1, min(X, n) + 1):
    ans = ans + findTotalPath(X - i, n, dp) % mod;
    ans %= mod;
 
  # Return total no of paths
  dp[X] = ans
  return ans
 
# Driver Code
if __name__ == '__main__':
     
  n = 3
  X = 2
 
  # Stores the number of ways
  # to obtains sums 0 to X
  dp = [-1] * (X + 1)
 
  # Function call
  print(findTotalPath(X, n, dp))
   
# This code is contributed by akhilsaini


C#
// C# program for the above approach
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG{
 
static int mod = (int)1e9 + 7;
 
// Function for counting total
// no of paths possible with
// the sum is equal to X
static int findTotalPath(int X, int n, int[] dp)
{
     
    // If the path of the sum
    // from the root to current
    // node is stored in sum
    if (X == 0)
    {
        return 1;
    }
    int ans = 0;
 
    // If already commputed
    if (dp[X] != -1)
    {
        return dp[X];
    }
 
    // Count different no of paths
    // using all possible ways
    for(int i = 1; i <= Math.Min(X, n); ++i)
    {
        ans += findTotalPath(X - i, n, dp) % mod;
        ans %= mod;
    }
 
    // Return total no of paths
    dp[X] = ans;
    return ans;
}
 
// Driver Code
public static void Main()
{
    int n = 3, X = 2;
     
    // Stores the number of ways
    // to obtains sums 0 to X
    int[] dp = new int[X + 1];
    Array.Fill(dp, -1);
 
    // Function call
    Console.WriteLine(findTotalPath(X, n, dp));
}
}
 
// This code is contributed by akhilsaini


输出:
2





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