using
System;
using
System.Collections.Generic;
public
class
DSU
{
private
List<
int
> parent;
public
DSU(
int
N)
{
parent =
new
List<
int
>(N + 1);
for
(
int
i = 0; i <= N; i++)
{
parent.Add(-1);
}
}
public
int
Get(
int
x)
{
return
parent[x] < 0 ? x : parent[x] = Get(parent[x]);
}
public
bool
SameSet(
int
a,
int
b)
{
return
Get(a) == Get(b);
}
public
int
Size(
int
x)
{
return
-parent[Get(x)];
}
public
bool
Union(
int
x,
int
y)
{
x = Get(x);
y = Get(y);
if
(x == y)
return
false
;
if
(parent[x] > parent[y])
{
int
temp = x;
x = y;
y = temp;
}
parent[x] += parent[y];
parent[y] = x;
return
true
;
}
}
public
class
Graph
{
public
static
void
IsGraphConnected(
int
N,
int
[,] edges,
int
M,
int
[] A)
{
DSU dsu =
new
DSU(N + 1);
List<List<
int
>> adj =
new
List<List<
int
>>(N + 1);
for
(
int
i = 0; i < N + 1; i++)
{
adj.Add(
new
List<
int
>());
}
for
(
int
i = 0; i < M; i++)
{
adj[edges[i, 0]].Add(edges[i, 1]);
adj[edges[i, 1]].Add(edges[i, 0]);
}
List<
int
> isInserted =
new
List<
int
>(N + 1);
for
(
int
i = 0; i <= N; i++)
{
isInserted.Add(0);
}
int
firstNodeToBeInserted = A[N - 1];
List<
string
> ans =
new
List<
string
>();
for
(
int
i = N - 1; i >= 0; i--)
{
if
(dsu.Size(firstNodeToBeInserted) == (N - 1 - i)
|| i == N - 1)
ans.Add(
"YES"
);
else
ans.Add(
"NO"
);
int
V = A[i];
isInserted[V] = 1;
foreach
(
var
U
in
adj[V])
{
if
(isInserted[U] == 1)
dsu.Union(V, U);
}
}
ans.Reverse();
foreach
(
var
item
in
ans)
{
Console.Write(item +
" "
);
}
Console.WriteLine();
}
}
public
class
Program
{
public
static
void
Main(
string
[] args)
{
int
N = 5, M = 5;
int
[,] edges = { { 1, 2 }, { 1, 3 }, { 3, 5 }, { 2, 5 }, { 5, 4 } };
int
[] A = { 3, 5, 2, 4, 1 };
Graph.IsGraphConnected(N, edges, M, A);
}
}