using
System;
using
System.Collections.Generic;
class
GFG
{
static
int
MAX_SIZE = 100005, MAX_CHAR = 26;
static
int
[,] nodecharsCount =
new
int
[MAX_SIZE, MAX_CHAR];
static
List<
int
>[] tree =
new
List<
int
>[MAX_SIZE];
static
bool
canFormPalindrome(
int
[] charArray)
{
int
oddCount = 0;
for
(
int
i = 0; i < MAX_CHAR; i++)
{
if
(charArray[i] % 2 == 1)
oddCount++;
}
if
(oddCount >= 2)
return
false
;
else
return
true
;
}
static
int
LCA(
int
currentNode,
int
x,
int
y)
{
if
(currentNode == x)
return
x;
if
(currentNode == y)
return
y;
int
xLca, yLca;
xLca = yLca = -1;
int
gotLca = -1;
for
(
int
l = 0; l < tree[currentNode].Count; l++)
{
int
nextNode = tree[currentNode][l];
int
out_ = LCA(nextNode, x, y);
if
(out_ == x)
xLca = out_;
if
(out_ == y)
yLca = out_;
if
(xLca != -1 && yLca != -1)
return
currentNode;
if
(out_ != -1)
gotLca = out_;
}
return
gotLca;
}
static
void
buildTree(
int
i)
{
for
(
int
l = 0; l < tree[i].Count; l++)
{
int
nextNode = tree[i][l];
for
(
int
j = 0; j < MAX_CHAR; j++)
{
nodecharsCount[nextNode,j] += nodecharsCount[i,j];
}
buildTree(nextNode);
}
}
static
bool
canFormPalindromicPath(
int
x,
int
y)
{
int
lcaNode;
if
(x == y)
lcaNode = x;
else
lcaNode = LCA(1, x, y);
int
[] charactersCountFromXtoY =
new
int
[MAX_CHAR];
for
(
int
i = 0; i < MAX_CHAR; i++)
{
charactersCountFromXtoY[i] = nodecharsCount[x, i] +
nodecharsCount[y, i]
- 2 * nodecharsCount[lcaNode, i];
}
if
(canFormPalindrome(charactersCountFromXtoY))
return
true
;
return
false
;
}
static
void
updateNodecharsCount(String str,
int
v)
{
for
(
int
i = 0; i < str.Length; i++)
nodecharsCount[v, str[i] -
'a'
]++;
}
static
void
performQueries(
int
[,] queries,
int
q) {
int
i = 0;
while
(i < q) {
int
x = queries[i, 0];
int
y = queries[i, 1];
if
(canFormPalindromicPath(x, y))
Console.WriteLine(
"Yes"
);
else
Console.WriteLine(
"No"
);
i++;
}
}
public
static
void
Main(String[] args)
{
for
(
int
i = 0; i < MAX_SIZE; i++)
{
for
(
int
j = 0; j < MAX_CHAR; j++)
{
nodecharsCount[i, j] = 0;
}
}
for
(
int
i = 0; i < MAX_SIZE; i++)
{
tree[i] =
new
List<
int
>();
}
tree[1].Add(2);
updateNodecharsCount(
"bbc"
, 2);
tree[1].Add(3);
updateNodecharsCount(
"ac"
, 3);
buildTree(1);
int
[,] queries = { { 1, 2 }, { 2, 3 }, { 3, 1 }, { 3, 3 } };
int
q = queries.GetLength(0);
performQueries(queries, q);
}
}