using
System;
using
System.Collections.Generic;
class
MainClass
{
static
int
tick = -1;
static
void
Dfs(List<
int
>[] adj,
int
u,
int
p,
int
[] depth,
int
[] tour,
int
[] inTime,
int
[] outTime)
{
tour[++tick] = depth[u];
inTime[u] = tick;
foreach
(
int
i
in
adj[u])
{
if
(i != p)
{
depth[i] = depth[u] + 1;
Dfs(adj, i, u, depth, tour, inTime, outTime);
}
}
tour[++tick] = depth[u];
outTime[u] = tick;
}
static
List<
int
> Solve(
int
N,
int
Q, List<
int
[]> Edge, List<
int
[]> query)
{
int
N2 = 2 * N;
List<
int
>[] adj =
new
List<
int
>[N];
for
(
int
i = 0; i < N; i++)
{
adj[i] =
new
List<
int
>();
}
List<
int
> ans =
new
List<
int
>();
int
[] depth =
new
int
[N];
int
[] euler =
new
int
[N2];
int
[] inTime =
new
int
[N];
int
[] outTime =
new
int
[N];
int
[] pref =
new
int
[N2];
int
[] suff =
new
int
[N2];
for
(
int
i = 0; i < Edge.Count; i++)
{
adj[Edge[i][0]].Add(Edge[i][1]);
adj[Edge[i][1]].Add(Edge[i][0]);
}
Dfs(adj, 0, -1, depth, euler, inTime, outTime);
pref[0] = euler[0];
suff[N2 - 1] = euler[N2 - 1];
for
(
int
i = 1; i < N2; i++)
{
pref[i] = Math.Max(pref[i - 1], euler[i]);
suff[N2 - i - 1] = Math.Max(suff[N2 - i], euler[N2 - i - 1]);
}
for
(
int
i = 0; i < query.Count; i++)
{
if
(depth[query[i][0]] < depth[query[i][1]])
{
int
temp = query[i][0];
query[i][0] = query[i][1];
query[i][1] = temp;
}
ans.Add(Math.Max(pref[inTime[query[i][0]] - 1],
suff[outTime[query[i][0]] + 1]));
}
return
ans;
}
public
static
void
Main(
string
[] args)
{
int
N = 3;
int
Q = 1;
List<
int
[]> Edge =
new
List<
int
[]> {
new
int
[] { 0, 1 },
new
int
[] { 0, 2 } };
List<
int
[]> query =
new
List<
int
[]> {
new
int
[] { 0, 1 } };
List<
int
> ans = Solve(N, Q, Edge, query);
int
i = 0;
while
(i < ans.Count)
{
Console.Write(ans[i] +
" "
);
i++;
}
}
}