using
System;
using
System.Collections;
using
System.Collections.Generic;
public
class
GFG {
static
void
dfs(
int
[] d,
int
node, List<
int
>[] adj,
int
dist)
{
d[node] = dist;
foreach
(
int
child
in
adj[node])
{
if
(dist + 1 < d[child])
dfs(d, child, adj, dist + 1);
}
}
static
int
minDist(
int
N,
int
[, ] edges)
{
int
ans = Int32.MaxValue;
List<
int
>[] adj =
new
List<
int
>[ N + 1 ];
for
(
int
i = 0; i <= N; i++)
adj[i] =
new
List<
int
>();
for
(
int
i = 0; i < edges.GetLength(0); i++) {
int
[] u = { edges[i, 0], edges[i, 1] };
adj[u[0]].Add(u[1]);
adj[u[1]].Add(u[0]);
}
for
(
int
i = 1; i <= N; i++) {
int
[] d =
new
int
[N + 1];
for
(
int
j = 0; j <= N; j++)
d[j] = Int32.MaxValue;
dfs(d, i, adj, 0);
int
dist = 0;
for
(
int
j = 1; j <= N; j++)
dist = Math.Max(dist, d[j]);
ans = Math.Min(ans, dist);
}
return
ans;
}
public
static
void
Main(
string
[] args)
{
int
N = 4;
int
[, ] edges = { { 1, 2 }, { 2, 3 }, { 2, 4 } };
Console.WriteLine(minDist(N, edges));
}
}