给定一棵有N个顶点的根树,一个数组values [] (表示分配给每个节点的值)和一个顶点V ,任务是计算从根目录开始的路径中的节点和直接邻居的值之和(总是0 )到V。
例子:
Input: N = 8, values = {1, 2, 3, 0, 0, 4, 3, 6}, V = 7
Output: 16
Explanation:
Path from root (0) to V (7) = 0 -> 1 -> 4 -> 7
Neighbors of 0 = (2, 3), Sum = 1(node 0) + 3(node 2) + 0(node 3) = 4
Neighbors of 1 = (5), Sum = 2(node 1) + 4(node 5) = 6
No neighbor of 4, Sum = 0(node 4) = 0
No neighbor of 7, Sum = 6(node 7) = 6
Total sum = 4 + 6 + 0 + 6 = 16
Input: N = 5, values = {5, 6, 2, 9, 0}, V = 2
Output: 13
方法:
想法是将每个节点的父级存储在数组中,并将每个父级的值与其子级相加,然后将其存储在父级节点中。现在,每个节点将保存其值与相应邻居的值之和。使用此数组查找从根到顶点V的路径的所需总和。
请按照以下步骤解决问题:
- 初始化一个数组,以使用DFS遍历存储每个节点及其对应邻居的值。
- 使用数组从顶点V迭代到根,并继续添加路径上找到的所有节点的值。
- 最后,打印获得的总和。
下面是上述方法的实现:
C++
// C++ Program to implement
// the above appraoch
#include
using namespace std;
// Creating Adjacency list
vector> constructTree(int n,
vector> edges)
{
vector> adjl(n);
for (auto e : edges)
{
int u = e[0];
int v = e[1];
adjl[u].push_back(v);
adjl[v].push_back(u);
}
return adjl;
}
// Function to perform DFS traversal
void DFS(vector> adjl,
vector &parent, int u, int p)
{
// Initializing parent of each node to p
parent[u] = p;
// Iterate over the children
for (int v : adjl[u])
{
if (v != p)
{
DFS(adjl, parent, v, u);
}
}
}
// Function to add values of children to
// respective parent nodes
vector valuesFromChildren(vector parent,
vector values)
{
vector valuesChildren(parent.size());
for (int i = 0; i < parent.size(); i++)
{
// Root node
if (parent[i] == -1)
continue;
else
{
int p = parent[i];
valuesChildren[p] += values[i];
}
}
return valuesChildren;
}
// Function to return sum of values of
// each node in path from V to the root
int findSumOfValues(int v, vector parent,
vector valuesChildren)
{
int cur_node = v;
int sum = 0;
// Path from node V to root node
while (cur_node != -1)
{
sum += valuesChildren[cur_node];
cur_node = parent[cur_node];
}
return sum;
}
// Driver Code
int main()
{
int n = 8;
// Insert edges into the graph
vector> edges = {{0, 1}, {0, 2}, {0, 3}, {1, 4},
{1, 5}, {4, 7}, {3, 6}};
int v = 7;
// Values assigned to each vertex
vector values = {1, 2, 3, 0, 0, 4, 3, 6};
// Constructing the tree
// using adjacency list
vector> adjl = constructTree(n, edges);
// Parent array
vector parent(n);
// store values in the parent array
DFS(adjl, parent, 0, -1);
// Add values of children to the parent
vector valuesChildren = valuesFromChildren(parent, values);
// Find sum of nodes lying in the path
int sum = findSumOfValues(v, parent, valuesChildren);
// Add root node since
// its value is not included yet
sum += values[0];
cout << sum << endl;
}
// This code is contributed by
// sanjeev2552
Java
// Java Program to implement
// the above appraoch
import java.io.*;
import java.util.*;
class GFG {
// Creating Adjacency list
private static List >
constructTree(int n, int[][] edges)
{
List > adjl
= new ArrayList >();
for (int i = 0; i < n; i++) {
adjl.add(new ArrayList());
}
for (int[] e : edges) {
int u = e[0];
int v = e[1];
adjl.get(u).add(v);
adjl.get(v).add(u);
}
return adjl;
}
// Function to perform DFS traversal
private static void DFS(
List > adjl,
int[] parent, int u, int p)
{
// Initializing parent of each node to p
parent[u] = p;
// Iterate over the children
for (int v : adjl.get(u)) {
if (v != p) {
DFS(adjl, parent, v, u);
}
}
}
// Function to add values of children to
// respective parent nodes
private static int[] valuesFromChildren(
int[] parent, int[] values)
{
int[] valuesChildren
= new int[parent.length];
for (int i = 0; i < parent.length; i++) {
// Root node
if (parent[i] == -1)
continue;
else {
int p = parent[i];
valuesChildren[p] += values[i];
}
}
return valuesChildren;
}
// Function to return sum of values of
// each node in path from V to the root
private static int findSumOfValues(
int v, int[] parent,
int[] valuesChildren)
{
int cur_node = v;
int sum = 0;
// Path from node V to root node
while (cur_node != -1) {
sum += valuesChildren[cur_node];
cur_node = parent[cur_node];
}
return sum;
}
// Driver Code
public static void main(String[] args)
{
int n = 8;
// Insert edges into the graph
int[][] edges = { { 0, 1 },
{ 0, 2 },
{ 0, 3 },
{ 1, 4 },
{ 1, 5 },
{ 4, 7 },
{ 3, 6 } };
int v = 7;
// Values assigned to each vertex
int[] values = new int[] { 1, 2, 3, 0,
0, 4, 3, 6 };
// Constructing the tree
// using adjacency list
List > adjl
= constructTree(n, edges);
// Parent array
int[] parent = new int[n];
// store values in the parent array
DFS(adjl, parent, 0, -1);
// Add values of children to the parent
int[] valuesChildren
= valuesFromChildren(parent, values);
// Find sum of nodes lying in the path
int sum = findSumOfValues(
v, parent,
valuesChildren);
// Add root node since
// its value is not included yet
sum += values[0];
System.out.println(sum);
}
}
C#
// C# program to implement
// the above appraoch
using System;
using System.Collections.Generic;
class GFG{
// Creating Adjacency list
private static List> constructTree(int n, int[,] edges)
{
List > adjl = new List >();
for(int i = 0; i < n; i++)
{
adjl.Add(new List());
}
for(int i = 0; i < edges.GetLength(0); i++)
{
int u = edges[i, 0];
int v = edges[i, 1];
adjl[u].Add(v);
adjl[v].Add(u);
}
return adjl;
}
// Function to perform DFS traversal
private static void DFS(List > adjl,
int[] parent, int u, int p)
{
// Initializing parent of each node to p
parent[u] = p;
// Iterate over the children
foreach(int v in adjl[u])
{
if (v != p)
{
DFS(adjl, parent, v, u);
}
}
}
// Function to add values of children to
// respective parent nodes
private static int[] valuesFromChildren(int[] parent,
int[] values)
{
int[] valuesChildren = new int[parent.Length];
for(int i = 0; i < parent.Length; i++)
{
// Root node
if (parent[i] == -1)
continue;
else
{
int p = parent[i];
valuesChildren[p] += values[i];
}
}
return valuesChildren;
}
// Function to return sum of values of
// each node in path from V to the root
private static int findSumOfValues(int v, int[] parent,
int[] valuesChildren)
{
int cur_node = v;
int sum = 0;
// Path from node V to root node
while (cur_node != -1)
{
sum += valuesChildren[cur_node];
cur_node = parent[cur_node];
}
return sum;
}
// Driver Code
public static void Main(string[] args)
{
int n = 8;
// Insert edges into the graph
int[, ] edges = { { 0, 1 }, { 0, 2 }, { 0, 3 },
{ 1, 4 }, { 1, 5 }, { 4, 7 },
{ 3, 6 } };
int v = 7;
// Values assigned to each vertex
int[] values = new int[] { 1, 2, 3, 0,
0, 4, 3, 6 };
// Constructing the tree
// using adjacency list
List> adjl = constructTree(n, edges);
// Parent array
int[] parent = new int[n];
// store values in the parent array
DFS(adjl, parent, 0, -1);
// Add values of children to the parent
int[] valuesChildren = valuesFromChildren(parent,
values);
// Find sum of nodes lying in the path
int sum = findSumOfValues(v, parent,
valuesChildren);
// Add root node since
// its value is not included yet
sum += values[0];
Console.WriteLine(sum);
}
}
// This code is contributed by ukasp
16
时间复杂度: O(N)
辅助空间: O(N)