-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfs.cpp
45 lines (34 loc) · 863 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
#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
void dfs(int start, vector<int> graph[], bool visited[]){
visited[start]= true;
cout<<start<<" ";
for(int i=0; i < graph[start].size(); i++){
int next = graph[start][i];
if(visited[next]==false)
dfs(next, graph, visited);
}
}
int main (){
int n;
int m; //number of line
cin >> n >> m;
vector<int> graph[n+1];
bool visited[n+1];
fill(visited, visited+n+1, false); //initialize the array to false
int x, y; //line
for(int i=0; i<m; i++){
cin >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
int start;
cin >> start;
for(int i=0; i<n; i++)
sort(graph[i].begin(), graph[i].end());
dfs(start, graph, visited);
return 0;
}