using
System;
using
System.Collections.Generic;
public
class
GFG {
static
void
DFS(
int
node,
Dictionary<
int
, List<
int
> > adj,
ref
string
col,
ref
HashSet<
char
>
set
,
ref
List<
bool
> vis)
{
vis[node] =
true
;
foreach
(
var
nbr
in
adj[node])
{
if
(!vis[nbr] && col[nbr] == col[node]) {
DFS(nbr, adj,
ref
col,
ref
set
,
ref
vis);
}
else
{
set
.Add(col[nbr]);
}
}
}
static
List<
string
> Solve(
int
N,
int
M,
int
Q,
string
col,
List<List<
int
> > Edge,
List<
string
> query)
{
List<
bool
> vis =
new
List<
bool
>(
new
bool
[N]);
int
[, , ] dp =
new
int
[26, 26, 26];
Dictionary<
int
, List<
int
> > adj
=
new
Dictionary<
int
, List<
int
> >();
foreach
(
var
i
in
Edge)
{
if
(!adj.ContainsKey(i[0]))
adj[i[0]] =
new
List<
int
>();
if
(!adj.ContainsKey(i[1]))
adj[i[1]] =
new
List<
int
>();
adj[i[0]].Add(i[1]);
adj[i[1]].Add(i[0]);
}
for
(
int
i = 0; i < N; i++) {
if
(!vis[i]) {
HashSet<
char
>
set
=
new
HashSet<
char
>();
DFS(i, adj,
ref
col,
ref
set
,
ref
vis);
foreach
(
var
it1
in
set
)
{
foreach
(
var
it2
in
set
)
{
dp[it1 -
'a'
, col[i] -
'a'
,
it2 -
'a'
]
= 1;
}
}
}
}
List<
string
> ans =
new
List<
string
>();
foreach
(
var
str
in
query)
{
int
a = str[0] -
'a'
;
int
b = str[1] -
'a'
;
int
c = str[2] -
'a'
;
if
(dp[a, b, c] != 0 || dp[a, c, b] != 0
|| dp[b, a, c] != 0 || dp[b, c, a] != 0
|| dp != 0 || dp != 0)
ans.Add(
"YES"
);
else
ans.Add(
"NO"
);
}
return
ans;
}
public
static
void
Main()
{
int
N = 6;
int
M = 7;
int
Q = 3;
string
col =
"abdebc"
;
List<List<
int
> > Edge =
new
List<List<
int
> >{
new
List<
int
>{ 0, 1 },
new
List<
int
>{ 0, 2 },
new
List<
int
>{ 1, 3 },
new
List<
int
>{ 1, 4 },
new
List<
int
>{ 1, 2 },
new
List<
int
>{ 2, 4 },
new
List<
int
>{ 4, 5 }
};
List<
string
> query
=
new
List<
string
>{
"abc"
,
"aec"
,
"azc"
};
List<
string
> ans = Solve(N, M, Q, col, Edge, query);
foreach
(
var
a
in
ans) { Console.Write(a +
" "
); }
}
}