import
java.util.*;
class
GFG{
static
void
addEdge(Vector<Integer> adj[],
int
u,
int
v)
{
adj[u].add(v);
adj[v].add(u);
}
static
void
printGraph(Vector<Integer> adj[],
int
V)
{
for
(
int
v =
0
; v < V; ++v)
{
System.out.print(
"\n Adjacency list of vertex "
+
v +
"\n head "
);
for
(
int
x : adj[v])
System.out.print(
". "
+ x);
System.out.printf(
"\n"
);
}
}
static
boolean
checkRingTopologyUtil(Vector<Integer> adj[],
int
V,
int
E)
{
if
(E != V)
return
false
;
if
(V <=
2
)
return
false
;
int
[] vertexDegree =
new
int
[V +
1
];
for
(
int
i =
1
; i <= V; i++)
{
for
(
int
v : adj[i])
{
vertexDegree[v]++;
}
}
int
countDegree2 =
0
;
for
(
int
i =
1
; i <= V; i++)
{
if
(vertexDegree[i] ==
2
)
{
countDegree2++;
}
}
if
(countDegree2 == V)
{
return
true
;
}
else
{
return
false
;
}
}
static
void
checkRingTopology(Vector<Integer> adj[],
int
V,
int
E)
{
boolean
isRing = checkRingTopologyUtil(adj, V, E);
if
(isRing)
{
System.out.print(
"YES"
+
"\n"
);
}
else
{
System.out.print(
"NO"
+
"\n"
);
}
}
public
static
void
main(String[] args)
{
int
V =
6
, E =
6
;
@SuppressWarnings
(
"unchecked"
)
Vector<Integer> []adj1 =
new
Vector[V +
1
];
for
(
int
i =
0
; i < adj1.length; i++)
adj1[i] =
new
Vector<Integer>();
addEdge(adj1,
1
,
2
);
addEdge(adj1,
2
,
3
);
addEdge(adj1,
3
,
4
);
addEdge(adj1,
4
,
5
);
addEdge(adj1,
6
,
1
);
addEdge(adj1,
5
,
6
);
checkRingTopology(adj1, V, E);
V =
5
; E =
4
;
@SuppressWarnings
(
"unchecked"
)
Vector<Integer> []adj2 =
new
Vector[V +
1
];
for
(
int
i =
0
; i < adj2.length; i++)
adj2[i] =
new
Vector<Integer>();
addEdge(adj2,
1
,
2
);
addEdge(adj2,
1
,
3
);
addEdge(adj2,
3
,
4
);
addEdge(adj2,
4
,
5
);
checkRingTopology(adj2, V, E);
}
}