forked from sunnyshahabuddin/Coding-Ninjas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsGraphATree.cpp
More file actions
81 lines (66 loc) · 1.11 KB
/
IsGraphATree.cpp
File metadata and controls
81 lines (66 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//Is the given graph a tree ?
// Two conditions
//1. Should not contain a cycle : no of edges = n-1
//2. 1 connected component
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define f(i, a, n) for (i = a; i < n; i++)
#define fe(i, a, n) for (i = a; i <= n; i++)
#define w(x) \
int x; \
cin >> x; \
while (x--)
#define mod 1000000007
#define ps(x, y) fixed << setprecision(y) << x
void sks()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
vector<int> adj[10001];
int vis[10001];
void dfs(int node)
{
vis[node] = 1;
for (int child : adj[node])
{
if (!vis[child])
dfs(child);
}
}
int main()
{
sks();
int n, m, a, b, i, cv = 0;
cin >> n >> m;
fe(i, 1, m)
{
cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
}
cv = 0;
fe(i, 1, n)
{
if (vis[i] == 0)
{
dfs(i);
cv++;
}
}
if (cv == 1 && m == n - 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}