Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- void dfs(int source, vector<bool> &visited, vector<vector<int> > &adj){
- //level = source
- //options = neighbours of source
- //mark source visited
- visited[source] = true;
- cout << source << " ";
- for(int i = 0; i < adj[source].size(); i++){
- int node = adj[source][i];
- if(!visited[node]){
- dfs(node, visited, adj);
- }
- }
- }
- int main() {
- // your code goes here
- int n, m;
- cin >> n >> m;
- vector<vector<int> > adj(n + 1, vector<int>());
- for(int i = 0; i < n; i++){
- int u, v;
- cin >> u >> v;
- adj[u].push_back(v);
- adj[v].push_back(u);
- }
- vector<bool> visited(n + 1, false);
- int source = 1;
- dfs(source, visited, adj);
- }
Advertisement
Add Comment
Please, Sign In to add comment