给定一棵具有特定权重的节点的n元树,我们的任务是找出每个节点从根到叶的除数最小差的最大和。
例子:
Input:
Output: 10
Explanation:
The maximum sum is along the path 18 -> 7 -> 12 -> 9
Minimum difference between divisors of 18 = 6 – 3 = 3
Minimum difference between divisors of 7 = 7 – 1 = 6
Minimum difference between divisors of 12 = 4 – 3 = 1
Minimum difference between divisors of 9 = 3 – 3 = 0
Input:
Output: 17
Explanation:
The maximum sum is along the path 20 -> 14 -> 26
方法:
为了解决上述问题,我们可以使用深度优先遍历存储数组中每个节点的除数之间的最小差。现在,任务是找出除数之间的最小差异。
- 我们可以观察到,对于任何数字N,在[√N,N]范围内的最小除数x在x和N / x之间的差异最小。
- 在每个节点,计算除数之间的最小差异,并且最后开始填写使用儿童的结果阵列和计算最大总和可能的。
下面是上述方法的实现:
C++
18
/ \
7 15
/ \ \
4 12 2
/
9
Java
20
/ \
13 14
/ \ \
10 8 26
/
25
Python3
// C++ program to maximize the sum of
// minimum difference of divisors
// of nodes in an n-ary tree
#include
using namespace std;
// Array to store the
// result at each node
int sub[100005];
// Function to get minimum difference
// between the divisors of a number
int minDivisorDifference(int n)
{
int num1;
int num2;
// Iterate from square
// root of N to N
for (int i = sqrt(n); i <= n; i++) {
if (n % i == 0) {
num1 = i;
num2 = n / i;
break;
}
}
// return absolute difference
return abs(num1 - num2);
}
// DFS function to calculate the maximum sum
int dfs(vector g[], int u, int par)
{
// Store the min difference
sub[u] = minDivisorDifference(u);
int mx = 0;
for (auto c : g[u]) {
if (c != par) {
int ans = dfs(g, c, u);
mx = max(mx, ans);
}
}
// Add the maximum of
// all children to sub[u]
sub[u] += mx;
// Return maximum sum of
// node 'u' to its parent
return sub[u];
}
// Driver code
int main()
{
vector g[100005];
int edges = 6;
g[18].push_back(7);
g[7].push_back(18);
g[18].push_back(15);
g[15].push_back(18);
g[15].push_back(2);
g[2].push_back(15);
g[7].push_back(4);
g[4].push_back(7);
g[7].push_back(12);
g[12].push_back(7);
g[12].push_back(9);
g[9].push_back(12);
int root = 18;
cout << dfs(g, root, -1);
}
C#
// Java program to maximize the sum of
// minimum difference of divisors
// of nodes in an n-ary tree
import java.util.Vector;
class GFG{
// Array to store the
// result at each node
static int []sub = new int[100005];
// Function to get minimum difference
// between the divisors of a number
static int minDivisorDifference(int n)
{
int num1 = 0;
int num2 = 0;
// Iterate from square
// root of N to N
for (int i = (int) Math.sqrt(n);
i <= n; i++)
{
if (n % i == 0)
{
num1 = i;
num2 = n / i;
break;
}
}
// return absolute difference
return Math.abs(num1 - num2);
}
// DFS function to calculate
// the maximum sum
static int dfs(Vector g[],
int u, int par)
{
// Store the min difference
sub[u] = minDivisorDifference(u);
int mx = 0;
for (int c : g[u])
{
if (c != par)
{
int ans = dfs(g, c, u);
mx = Math.max(mx, ans);
}
}
// Add the maximum of
// all children to sub[u]
sub[u] += mx;
// Return maximum sum of
// node 'u' to its parent
return sub[u];
}
// Driver code
public static void main(String[] args)
{
@SuppressWarnings("unchecked")
Vector []g =
new Vector[100005];
for (int i = 0; i < g.length; i++)
g[i] = new Vector();
int edges = 6;
g[18].add(7);
g[7].add(18);
g[18].add(15);
g[15].add(18);
g[15].add(2);
g[2].add(15);
g[7].add(4);
g[4].add(7);
g[7].add(12);
g[12].add(7);
g[12].add(9);
g[9].add(12);
int root = 18;
System.out.print(dfs(g, root, -1));
}
}
// This code is contributed by Princi Singh
输出:
# Python3 program to maximize the sum
# of minimum difference of divisors
# of nodes in an n-ary tree
import math
# Array to store the
# result at each node
sub = [0 for i in range(100005)]
# Function to get minimum difference
# between the divisors of a number
def minDivisorDifference(n):
num1 = 0
num2 = 0
# Iterate from square
# root of N to N
for i in range(int(math.sqrt(n)), n + 1):
if (n % i == 0):
num1 = i
num2 = n // i
break
# Return absolute difference
return abs(num1 - num2)
# DFS function to calculate the maximum sum
def dfs(g, u, par):
# Store the min difference
sub[u] = minDivisorDifference(u)
mx = 0
for c in g[u]:
if (c != par):
ans = dfs(g, c, u)
mx = max(mx, ans)
# Add the maximum of
# all children to sub[u]
sub[u] += mx
# Return maximum sum of
# node 'u' to its parent
return sub[u]
# Driver code
if __name__=='__main__':
g = [[] for i in range(100005)]
edges = 6
g[18].append(7)
g[7].append(18)
g[18].append(15)
g[15].append(18)
g[15].append(2)
g[2].append(15)
g[7].append(4)
g[4].append(7)
g[7].append(12)
g[12].append(7)
g[12].append(9)
g[9].append(12)
root = 18
print(dfs(g, root, -1))
# This code is contributed by rutvik_56