using
System;
using
System.Collections.Generic;
public
class
GFG{
static
int
timer = 1;
static
void
dfs(List<
int
> []adj,
int
[]itime,
int
[]otime,
int
src,
int
par)
{
itime[src] = timer++;
foreach
(
int
it
in
adj[src]) {
if
(it != par) {
dfs(adj, itime, otime, it, src);
}
}
otime[src] = timer++;
}
static
bool
check(
int
[]itime,
int
[]otime,
int
x,
int
y)
{
if
(itime[x] < itime[y]
&& otime[x] > otime[y])
return
true
;
return
false
;
}
static
void
solve(
int
N,
int
[,] edges,
int
Q,
int
[,] query)
{
List<
int
> []adj =
new
List<
int
>[N + 1];
for
(
int
i = 0; i < adj.Length; i++)
adj[i] =
new
List<
int
>();
for
(
int
i = 0; i < N - 1; i++) {
adj[edges[i,0]].Add(edges[i,1]);
adj[edges[i,1]].Add(edges[i,0]);
}
int
[]intime =
new
int
[N + 1];
int
[]outtime =
new
int
[N + 1];
dfs(adj, intime, outtime, 1, -1);
for
(
int
i = 0; i < Q; i++) {
int
type = query[i,0],
X = query[i,1],
Y = query[i,2];
if
(type == 0) {
if
(check(intime, outtime, X, Y)) {
Console.Write(
"Yes"
+
"\n"
);
}
else
{
Console.Write(
"No"
+
"\n"
);
}
}
else
{
if
(check(intime, outtime, Y, X)) {
Console.Write(
"Yes"
+
"\n"
);
}
else
{
Console.Write(
"No"
+
"\n"
);
}
}
}
}
public
static
void
Main(String[] args)
{
int
N = 9;
int
[,] edges
= { { 1, 2 }, { 1, 3 }, { 2, 6 },
{ 2, 7 }, { 6, 9 }, { 7, 8 },
{ 3, 4 }, { 3, 5 } };
int
Q = 5;
int
[,] query = { { 0, 2, 8 },
{ 1, 2, 8 },
{ 1, 6, 5 },
{ 0, 6, 5 },
{ 1, 9, 1 } };
solve(N, edges, Q, query);
}
}