using
System;
using
System.Collections.Generic;
class
GFG{
static
List<List<
int
>> v;
static
Dictionary<
int
,
int
> mp;
static
List<
int
> a;
static
void
dfs(
int
[]under,
int
child,
int
parent)
{
a.Add(child);
under[child] = 1;
foreach
(
int
it
in
v[child])
{
if
(it != parent)
{
dfs(under, it, child);
under[child] += under[it];
}
}
}
static
int
printnodeDFSofSubtree(
int
node,
int
[]under,
int
m)
{
int
ind = mp[node];
return
a[ind + m - 1];
}
static
void
addEdge(
int
x,
int
y)
{
v[x].Add(y);
v[y].Add(x);
}
static
void
markIndexDfs()
{
int
size = a.Count;
for
(
int
i = 0; i < size; i++)
{
mp.Add(a[i], i);
}
}
public
static
void
Main(String[] args)
{
int
n = 7;
mp =
new
Dictionary<
int
,
int
>();
v =
new
List<List<
int
>>();
a =
new
List<
int
>();
for
(
int
i = 0; i < n + 1; i++)
v.Add(
new
List<
int
>());
addEdge(1, 2);
addEdge(1, 3);
addEdge(2, 4);
addEdge(2, 5);
addEdge(4, 6);
addEdge(4, 7);
int
[]under =
new
int
[n + 1];
dfs(under, 1, 0);
markIndexDfs();
int
m = 3;
Console.WriteLine(
printnodeDFSofSubtree(1, under, m));
m = 4;
Console.WriteLine(
printnodeDFSofSubtree(2, under, m));
}
}