Skip to content

Instantly share code, notes, and snippets.

@Cee
Created November 18, 2014 01:13
Show Gist options
  • Save Cee/766953d7c238c8d9c1ad to your computer and use it in GitHub Desktop.
Save Cee/766953d7c238c8d9c1ad to your computer and use it in GitHub Desktop.
DFS
#include <iostream>
using namespace std;
int n, m;
bool vector[100][100];
bool visited[100];
void dfs(int point){
cout << (point + 1);
visited[point] = true;
for (int j = 0; j < n; j++){
if (!visited[j] && vector[point][j]){
dfs(j);
}
}
}
int main(){
cin >> n >> m;
for (int i = 0; i < m; i++){
int x, y;
cin >> x >> y;
vector[x - 1][y - 1] = true;
vector[y - 1][x - 1] = true;
}
for (int i = 0; i < n; i++){
if (!visited[i]){
dfs(i);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment