using
System;
using
System.Collections.Generic;
class
Graph {
static
void
createGraph(List<List<
int
> > graph,
List<List<
int
> > edges)
{
foreach
(List<
int
> x
in
edges)
{
graph[x[0]].Add(x[1]);
graph[x[1]].Add(x[0]);
}
}
static
bool
findSimpleCycle(
int
cur,
List<List<
int
> > graph,
int
start,
int
dest,
bool
[] visited,
bool
flag)
{
if
(!flag && cur == dest) {
dest = start;
flag =
true
;
}
if
(flag && cur == dest) {
return
true
;
}
bool
ans =
false
;
visited[cur] =
true
;
foreach
(
int
child
in
graph[cur])
{
if
(!visited[child] || child == dest) {
ans = ans
| findSimpleCycle(child, graph, start,
dest, visited,
flag);
}
}
visited[cur] =
false
;
return
ans;
}
public
static
void
Main(
string
[] args)
{
int
nodes = 7;
List<List<
int
> > edges =
new
List<List<
int
> >() {
new
List<
int
>() { 0, 1 },
new
List<
int
>() { 1, 2 },
new
List<
int
>() { 2, 3 },
new
List<
int
>() { 3, 4 },
new
List<
int
>() { 4, 5 },
new
List<
int
>() { 5, 2 },
new
List<
int
>() { 2, 6 },
new
List<
int
>() { 6, 0 }
};
int
S = 0, T = 4;
List<List<
int
> > graph =
new
List<List<
int
> >();
for
(
int
i = 0; i < nodes; i++) {
graph.Add(
new
List<
int
>());
}
bool
[] visited =
new
bool
[nodes];
createGraph(graph, edges);
if
(findSimpleCycle(S, graph, S, T, visited,
false
)) {
Console.WriteLine(
"Yes"
);
}
else
{
Console.WriteLine(
"No"
);
}
}
}