-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnyPath.cpp
74 lines (59 loc) · 931 Bytes
/
AnyPath.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
64
65
66
67
68
69
70
71
72
73
74
#include<bits/stdc++.h>
using namespace std;
vector<list<int>> graph;
unordered_set<int> s;
int v;
void addEdge(int src,int dstn,bool bidrn = true){
graph[src].push_back(dstn);
if(bidrn){
graph[dstn].push_back(src);
}
}
bool DFS(int src,int end){
s.insert(src);
if(src == end) return true;
bool ans = false;
for(auto x : graph[src]){
if(s.find(x) == s.end()){
ans = ans || DFS(x,end);
}
}
return ans;
}
void display(){
for(int i=0;i<graph.size();i++){
cout<<i<<" -> ";
for(int elem : graph[i]){
cout<<elem<<" ";
}
cout<<endl;
}
}
/*
7
8
0 1
0 4
3 1
3 4
1 5
5 6
5 2
2 6
0 6
1
*/
int main(){
cin>>v;
graph.resize(v,list<int> ());
int e;
cin>>e;
while(e--){
int s,d;
cin>>s>>d;
addEdge(s,d);
}
int x,y;
cin>>x>>y;
cout<<DFS(x,y);
}