Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- const int MAX = 100;
- vector<int> adj[MAX];
- bool visited[MAX];
- void DFS(int u) {
- visited[u] = true;
- cout << u << " ";
- for (int i = 0; i < adj[u].size(); i++) {
- int v = adj[u][i];
- if (!visited[v]) {
- DFS(v);
- }
- }
- }
- int main() {
- int n, m, s;
- cin >> n >> m >> s;
- for (int i = 0; i < m; i++) {
- int u, v;
- cin >> u >> v;
- adj[u].push_back(v);
- adj[v].push_back(u);
- }
- DFS(s);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement