forked from Harshita-Kanal/Data-Structures-and-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cpp-IsItATree.cpp
57 lines (51 loc) · 885 Bytes
/
Cpp-IsItATree.cpp
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
#include<iostream>
#include<algorithm>
#include<vector>
#include<iterator>
using namespace std;
#define endl "\n"
#define ll long long
const int N = 1e5 + 5;
const unsigned int M = 1000000007;
ll a[N],b[N];
const int MX = 100010; // maximum no. of nodes in graph
vector<bool> visited(MX+1,0);
vector<int>G[MX+1];
void addedge(int u,int v)
{
G[u].push_back(v);
G[v].push_back(u);
}
void dfs(int x)
{
visited[x]=true;
for(auto& i : G[x])
{
if(visited[i]==false)
dfs(i);
}
}
int32_t main ()
{
int n,m,t; cin>>n>>m;
t=m;
while(t--)
{
int u,v; cin>>u>>v;
addedge(u,v);
}
int count=0;
for(int i=1;i<=n;i++)
{
if(!visited[i])
{
dfs(i);
count++;
}
}
if(m==n-1 and count==1)
cout<<"YES";
else
cout<<"NO";
return 0;
}