-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sky Islands (AL).cpp
63 lines (54 loc) · 1.33 KB
/
Sky Islands (AL).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
58
59
60
61
62
63
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <set>
#include <unordered_map>
#include <list>
using namespace std;
unordered_map<int, bool>visited; //initialising visited array
unordered_map<int, list<int>>AL; //initialising Adjacency List
void add_edge(int x, int y){ //add edge for undirected
AL[x].push_back(y);
AL[y].push_back(x);
}
void DFS(int V){ //DFS Function
visited[V] = true;
//cout << V; //if want to print traversal
list<int>::iterator i;
for (i = AL[V].begin(); i != AL[V].end(); ++i)
if (!visited[*i])
DFS(*i);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int V; int M; cin >> V >> M;
if (V==1){
cout<<"YES";
return 0;
}
bool output = true;
for (int i = 0; i < M; i++){
int x; int y; cin>> x >> y;
add_edge( x, y);
visited[x] = false;
visited[y] = false;
}
DFS(AL.begin()->first);
for (auto i : visited){
if (i.second==false){
output= false;
}
else{
output=true;
}
}
if (output){
cout << "YES";
}
else{
cout << "NO";
}
}