-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdepth-first-traversal.cpp
61 lines (45 loc) · 1.07 KB
/
depth-first-traversal.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
#include <iostream>
#include <unordered_map>
#include <climits>
#include <list>
#include <vector>
#include <stack>
#include <cstring>
#include <queue>
using namespace std;
#define ll long long
void printutil(int n, vector<int> al[]){
for(int i=1; i<n+1; i++){
cout<<i<<" -> ";
for(int j=0; j<al[i].size(); j++){
cout<<al[i][j]<<" ";
}
cout<<'\n';
}
}
void getinput(int m, vector<int> al[]){
for(int i=0; i<m; i++){
int x,y; cin>>x>>y;
al[x].push_back(y);
al[y].push_back(x);
}
}
void solve(bool visited[], int s, vector<int> al[]){
visited[s]=true;
//q.push(s);
cout<<s<<" ";
for(int i=0; i<al[s].size(); i++){
if(!visited[al[s][i]]) solve(visited, al[s][i], al);
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, k; cin>>n>>m>>k; //k is starting vertex;
vector<int> al[n+1];
bool visited[n+1]; memset(visited, false, sizeof(visited));
getinput(m, al);
solve(visited, k, al); cout<<endl;
printutil(n, al);
return 0;
}