-
Notifications
You must be signed in to change notification settings - Fork 0
/
089_GetDFSPath.cpp
68 lines (52 loc) · 1.37 KB
/
089_GetDFSPath.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
#include<bits/stdc++.h>
using namespace std;
map<int, vector<int>> edges;
vector<int> path;
bool dfs(int e, vector<int> &subset, vector<bool> &visited, int v2){
if(e==v2){
path=vector<int>(subset);
return true;
}
if(visited[e]){
return false;
}
visited[e]=true;
for(auto E: edges[e]){
subset.push_back(E);
bool flag = dfs(E, subset, visited, v2);
if(flag){
return true;
}
subset.pop_back();
}
return false;
}
vector<int> getPath(int V, int E, vector<int> a, vector<int> b, int v1, int v2) {
// Write your code here
for(int i=0; i<V; i++){
edges[i]={};
}
for(int i=0; i<a.size(); i++){
edges[a[i]].push_back(b[i]);
edges[b[i]].push_back(a[i]);
}
vector<int> subset;
subset.push_back(v1);
vector<bool> visited(V, false);
for(auto E: edges[v1]){
visited=vector<bool>(V, false);
visited[v1]=true;
subset.push_back(E);
bool flag=dfs(E, subset, visited, v2);
if(flag){
reverse(path.begin(), path.end());
// for(auto p:path){
// cout<<p<<" ";
// }
// cout<<endl;
return path;
}
subset.pop_back();
}
return {};
}