using
System;
using
System.Collections.Generic;
class
GFG{
static
readonly
int
maxN = 100001;
static
List<
int
> []adj =
new
List<
int
>[maxN];
static
int
[]height =
new
int
[maxN];
static
int
[]dist =
new
int
[maxN];
static
void
addEdge(
int
u,
int
v)
{
adj[u].Add(v);
adj[v].Add(u);
}
static
void
dfs1(
int
cur,
int
par)
{
foreach
(
int
u
in
adj[cur])
{
if
(u != par)
{
dfs1(u, cur);
height[cur] = Math.Max(height[cur],
height[u]);
}
}
height[cur] += 1;
}
static
void
dfs2(
int
cur,
int
par)
{
int
max1 = 0;
int
max2 = 0;
foreach
(
int
u
in
adj[cur])
{
if
(u != par)
{
if
(height[u] >= max1)
{
max2 = max1;
max1 = height[u];
}
else
if
(height[u] > max2)
{
max2 = height[u];
}
}
}
int
sum = 0;
foreach
(
int
u
in
adj[cur])
{
if
(u != par)
{
sum = ((max1 == height[u]) ?
max2 : max1);
if
(max1 == height[u])
dist[u] = 1 + Math.Max(1 + max2,
dist[cur]);
else
dist[u] = 1 + Math.Max(1 + max1,
dist[cur]);
dfs2(u, cur);
}
}
}
public
static
void
Main(String[] args)
{
int
n = 6;
for
(
int
i = 0; i < adj.Length; i++)
adj[i] =
new
List<
int
>();
addEdge(1, 2);
addEdge(2, 3);
addEdge(2, 4);
addEdge(2, 5);
addEdge(5, 6);
dfs1(1, 0);
dfs2(1, 0);
for
(
int
i = 1; i <= n; i++)
Console.Write((Math.Max(dist[i],
height[i]) - 1) +
" "
);
}
}