using
System;
using
System.Collections.Generic;
public
class
GFG
{
static
int
curMax;
static
int
dfs(List<
int
> []g,
int
u,
int
v)
{
int
max1 = 0, max2 = 0, total = 0;
for
(
int
i = 0; i < g[u].Count; i++)
{
if
(g[u][i] == v)
continue
;
total = Math.Max(total, dfs(
g, g[u][i], u));
if
(curMax > max1)
{
max2 = max1;
max1 = curMax;
}
else
max2 = Math.Max(max2, curMax);
}
total = Math.Max(total, max1 + max2);
curMax = max1 + 1;
return
total;
}
static
int
maxProductOfTwoPaths(List<
int
> []g,
int
N)
{
int
res =
int
.MinValue;
int
path1, path2;
for
(
int
i = 1; i < N + 2; i++)
{
for
(
int
j = 0; j < g[i].Count; j++)
{
curMax = 0;
path1 = dfs(g, g[i][j], i);
curMax = 0;
path2 = dfs(g,i, g[i][j]);
res = Math.Max(res, path1 * path2);
}
}
return
res;
}
static
void
addEdge(List<
int
> []g,
int
u,
int
v)
{
g[u].Add(v);
g[v].Add(u);
}
public
static
void
Main(String[] args)
{
int
[,]edges = { { 1, 8 }, { 2, 6 },
{ 3, 1 }, { 5, 3 },
{ 7, 8 }, { 8, 4 },
{ 8, 6 } };
int
N = edges.GetLength(0);
List<
int
> []g =
new
List<
int
>[N + 2];
for
(
int
i = 0; i < g.Length; i++)
g[i] =
new
List<
int
>();
for
(
int
i = 0; i < N; i++)
addEdge(g, edges[i,0], edges[i,1]);
Console.Write(maxProductOfTwoPaths(g, N) +
"\n"
);
}
}