-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathget_pathDSF.cpp
More file actions
67 lines (51 loc) · 1.2 KB
/
get_pathDSF.cpp
File metadata and controls
67 lines (51 loc) · 1.2 KB
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
#include<bits/stdc++.h>
using namespace std;
bool DFS(bool **edge,bool *visited,vector<int> &vect,int sv,int v2,int n)
{
visited[sv]=true;
if(visited[v2]) return true;
for(int i=0 ; i<n ; i++)
{
if(edge[sv][i] && (!visited[i])){
vect.push_back(i);
bool flag = DFS(edge,visited,vect,i,v2,n);
if(flag==true)
return true;
else
vect.pop_back();
}
}
return false;
}
int main()
{
int n,e;
cin>>n>>e;
bool **edge=new bool*[n];
for(int i=0 ; i<n ; i++) edge[i]=new bool[n]();
bool *visited=new bool[n]();
while(e--)
{
int f,s; cin>>f>>s;
edge[f][s]=edge[s][f]=true;
}
int v1,v2;
cin>>v1>>v2;
if(v1==v2)
{
cout<<v1<<endl;
return 0 ;
}
vector<int> probable_list;
probable_list.push_back(v1);
if( DFS(edge,visited,probable_list,v1,v2,n))
{
reverse(probable_list.begin(),probable_list.end());
for(auto i : probable_list)
cout<<i<<" ";
}
for(int i=0 ; i<n ; i++)
delete[] edge[i];
delete[] edge;
delete[] visited;
}