using System;
using System.Collections.Generic;
class Program
{
static int BreadthFirst(int v, List<List<int>> graph, bool[] visited, int[] values)
{
int sumValue = 0;
visited[v] = true;
Queue<int> q = new Queue<int>();
q.Enqueue(v);
while (q.Count > 0)
{
int node = q.Dequeue();
sumValue += values[node - 1];
foreach (int neighbor in graph[node])
{
if (!visited[neighbor])
{
q.Enqueue(neighbor);
visited[neighbor] = true;
}
}
}
return sumValue;
}
static void MaximumSumOfValues(List<List<int>> graph, int vertices, int[] values)
{
bool[] visited = new bool[vertices + 1];
int maxSum = int.MinValue;
for (int i = 1; i <= vertices; i++)
{
if (!visited[i])
{
int currentComponentSum = BreadthFirst(i, graph, visited, values);
maxSum = Math.Max(maxSum, currentComponentSum);
}
}
Console.WriteLine("Max Sum Value = " + maxSum);
}
static void Main()
{
List<List<int>> graph = new List<List<int>>(1001);
for (int i = 0; i < 1001; i++)
{
graph.Add(new List<int>());
}
int V = 10;
int[] values = { 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 };
graph[2].Add(3);
graph[3].AddRange(new[] { 2, 4 });
graph[4].AddRange(new[] { 3, 5 });
graph[5].Add(4);
graph[6].Add(7);
graph[7].AddRange(new[] { 6, 8 });
graph[8].Add(7);
graph[9].Add(10);
graph[10].Add(9);
MaximumSumOfValues(graph, V, values);
}
}