Created
November 18, 2014 01:13
-
-
Save Cee/766953d7c238c8d9c1ad to your computer and use it in GitHub Desktop.
DFS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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