using
System;
using
System.Collections.Generic;
class
GFG{
static
Dictionary<
int
,
List<
int
>> tree =
new
Dictionary<
int
,
List<
int
>>();
static
Dictionary<
int
,
int
> vis =
new
Dictionary<
int
,
int
>();
static
bool
valid_bfs(List<
int
> v)
{
int
n = (
int
)v.Count;
Queue<HashSet<
int
>> q =
new
Queue<HashSet<
int
>>();
HashSet<
int
> s =
new
HashSet<
int
>();
s.Add(1);
q.Enqueue(s);
int
i = 0;
while
(q.Count != 0 && i < n)
{
if
(vis.ContainsKey(v[i]))
{
return
false
;
}
vis.Add(v[i], 1);
if
(q.Peek().Count == 0)
{
q.Dequeue();
}
if
(!q.Peek().Contains(v[i]))
{
return
false
;
}
s.Clear();
foreach
(
int
j
in
tree[v[i]])
{
if
(vis.ContainsKey(j))
{
continue
;
}
s.Add(j);
}
if
(s.Count > 0)
{
HashSet<
int
> temp = s;
q.Enqueue(temp);
}
s.Clear();
q.Peek().Remove(v[i]);
i++;
}
return
true
;
}
public
static
void
Main(String[] args)
{
for
(
int
i = 1; i <= 6; i++)
{
tree.Add(i,
new
List<
int
>());
}
tree[1].Add(2);
tree[2].Add(1);
tree[1].Add(5);
tree[5].Add(1);
tree[2].Add(3);
tree[3].Add(2);
tree[2].Add(4);
tree[4].Add(2);
tree[5].Add(6);
tree[6].Add(5);
int
[]arr1 = {1, 5, 2, 3, 4, 6};
List<
int
> arr =
new
List<
int
>();
arr.AddRange(arr1);
if
(valid_bfs(arr))
Console.Write(
"Yes"
+
"\n"
);
else
Console.Write(
"No"
+
"\n"
);
}
}