using
System;
using
System.Collections.Generic;
public
class
Graph {
public
int
V;
public
List<
int
>[] adj;
public
void
DFSUtil(
int
v,
bool
[] visited,
int
[] id,
int
id_number,
int
[] component_size)
{
visited[v] =
true
;
id[v] = id_number;
component_size[id_number]++;
foreach
(
int
i
in
adj[v])
{
if
(!visited[i]) {
DFSUtil(i, visited, id, id_number,
component_size);
}
}
}
public
bool
connectedComponents()
{
bool
[] visited =
new
bool
[V + 1];
int
[] id =
new
int
[V + 1];
int
[] component_size =
new
int
[V + 1];
for
(
int
v = 1; v <= V; v++) {
visited[v] =
false
;
}
for
(
int
v = 1; v <= V; v++) {
if
(!visited[v]) {
int
id_number = v;
DFSUtil(v, visited, id, id_number,
component_size);
}
}
for
(
int
v = 1; v <= V; v++) {
if
(component_size[id[v]] - 1 != adj[v].Count) {
return
false
;
}
}
return
true
;
}
public
Graph(
int
V)
{
this
.V = V;
adj =
new
List<
int
>[ V + 1 ];
for
(
int
i = 0; i <= V; i++) {
adj[i] =
new
List<
int
>();
}
}
public
void
addEdge(
int
v,
int
w)
{
adj[v].Add(w);
adj[w].Add(v);
}
}
public
class
Program {
static
void
isTransitive(
int
N,
int
M,
int
[][] Edge)
{
Graph G =
new
Graph(N);
for
(
int
i = 0; i < M; i++) {
G.addEdge(Edge[i][0], Edge[i][1]);
}
if
(G.connectedComponents()) {
Console.WriteLine(
"YES"
);
}
else
{
Console.WriteLine(
"NO"
);
}
}
public
static
void
Main(
string
[] args)
{
int
N = 4, M = 3;
int
[][] Edge =
new
int
[3][];
Edge[0] =
new
int
[] { 1, 3 };
Edge[1] =
new
int
[] { 3, 4 };
Edge[2] =
new
int
[] { 1, 4 };
isTransitive(N, M, Edge);
}
}