-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfs.cpp
48 lines (45 loc) · 992 Bytes
/
dfs.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
#include<iostream>
#include<vector>
#include<unordered_set>
#include<list>
using namespace std;
// This is the visited part
unordered_set<int> visited;
vector<list<int>> graph;
int v; // No of Vertices
void add_edge(int src, int dest, bool bi_dir = true) {
graph[src].push_back(dest);
if(bi_dir) {
graph[dest].push_back(src);
}
}
bool dfs(int curr, int end) {
if(curr == end) return true;
visited.insert(curr); // Mark Visited
for(auto neighbour : graph[curr]) {
if(not visited.count(neighbour)) {
bool result = dfs(neighbour, end);
if(result) return true;
}
}
return false;
}
bool anyPath(int src, int dest) {
return dfs(src, dest);
}
int main() {
cin>>v;
graph.resize(v, list<int> ());
int e;
cin>>e;
visited.clear();
while(e--) {
int s,d;
cin>>s>>d;
add_edge(s,d, false);
}
int x, y;
cin>>x>>y;
cout<<anyPath(x,y) <<"/n";
return 0;
}