using
System;
using
System.Collections.Generic;
class
GFG{
static
bool
[] visited =
new
bool
[10001];
static
List<
int
>storeChain =
new
List<
int
>();
static
void
depthFirst(
int
v,
List<
int
> []graph)
{
visited[v] =
true
;
storeChain.Add(v);
foreach
(
int
i
in
graph[v])
{
if
(visited[i] ==
false
)
{
depthFirst(i, graph);
}
}
}
static
void
checkPalin(
int
[]arr,
int
n)
{
Dictionary<
int
,
int
> frequency =
new
Dictionary<
int
,
int
>();
HashSet<
int
> element =
new
HashSet<
int
>();
for
(
int
i = 0; i < n; i ++)
{
if
((element.Contains(arr[i])))
{
frequency[arr[i]]++;
}
else
{
frequency.Add(arr[i], 1);
}
element.Add(arr[i]);
}
int
result = 1;
if
(n % 2 == 0)
{
foreach
(KeyValuePair<
int
,
int
> i
in
frequency)
{
if
(i.Value % 2 != 0)
{
result = 0;
break
;
}
}
}
else
{
int
countFreq = 0;
foreach
(KeyValuePair<
int
,
int
> i
in
frequency)
{
if
(i.Value % 2 != 0)
{
countFreq ++;
}
}
if
(countFreq != 1)
result = 0;
}
if
(result == 0)
Console.Write(
"longest connected "
+
"component is Palindrome"
);
else
Console.Write(
"longest connected "
+
"component not a Palindrome"
);
}
static
void
longestConnectionPalin(List<
int
> []graph,
int
vertices,
List<
int
> values)
{
int
maxChain = 0;
List<
int
> maxStoreChain =
new
List<
int
>();
for
(
int
i = 1; i <= vertices; i++)
{
if
(visited[i] ==
false
)
{
int
sizeChain;
depthFirst(i, graph);
sizeChain = storeChain.Count;
if
(sizeChain > maxChain)
{
maxChain = sizeChain;
maxStoreChain = storeChain;
}
}
}
int
[]longChainValues =
new
int
[maxChain + 1];
for
(
int
i = 0; i < maxChain; i ++)
{
int
temp = values[maxStoreChain[i] - 1];
longChainValues[i] = temp;
}
checkPalin(longChainValues,
maxChain);
}
public
static
void
Main(String[] args)
{
List<
int
> []graph =
new
List<
int
>[1001];
for
(
int
i = 0; i < graph.Length; i++)
graph[i] =
new
List<
int
>();
int
V;
V = 7;
List<
int
> values =
new
List<
int
>();
values.Add(10);
values.Add(25);
values.Add(5);
values.Add(15);
values.Add(5);
values.Add(20);
values.Add(0);
graph[1].Add(2);
graph[2].Add(1);
graph[3].Add(4);
graph[4].Add(3);
graph[3].Add(5);
graph[5].Add(3);
graph[6].Add(7);
graph[7].Add(6);
longestConnectionPalin(graph, V, values);
}
}