using
System;
using
System.Collections.Generic;
class
GFG
{
static
Dictionary<
int
,
int
> sum =
new
Dictionary<
int
,
int
>();
static
int
depth = 0;
static
void
add_edge(
int
a,
int
b, List<List<
int
>> tree)
{
a--;
b--;
tree[a].Add(b);
tree[b].Add(a);
}
static
void
dfs(
int
u,
int
level,
int
par,
int
[]node_values, List<List<
int
>> tree
)
{
depth = Math.Max(depth, level);
if
(sum.ContainsKey(level))
sum[level] += node_values[u];
else
sum[level] = node_values[u];
foreach
(
int
child
in
tree[u]) {
if
(child == par)
continue
;
dfs(child, level + 1, u, node_values,
tree);
}
}
static
void
getSum(
int
[]node_values, List<List<
int
>> tree)
{
dfs(0, 0, -1, node_values, tree);
for
(
int
i = 0; i <= depth; i++) {
Console.WriteLine(
"Sum of level "
+ (
int
) i +
" = "
+ sum[i]);
}
}
public
static
void
Main()
{
int
N = 10;
List<List<
int
> > tree =
new
List<List<
int
>>();
for
(
int
i = 0; i < N; i++)
tree.Add(
new
List<
int
>());
add_edge(1, 2, tree);
add_edge(1, 3, tree);
add_edge(2, 4, tree);
add_edge(3, 5, tree);
add_edge(3, 8, tree);
add_edge(5, 6, tree);
add_edge(5, 7, tree);
add_edge(8, 9, tree);
add_edge(8, 10, tree);
int
[]node_values = {2, 3, 4, 4, 7,6, 2, 3, 9, 1};
getSum(node_values, tree);
}
}