Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- vector<int>adj[100];
- stack<int>stk;
- bool visited[100];
- void dfs(int src)
- {
- stk.push(src);
- visited[src]=true;
- cout<<src<<" ";
- while(!stk.empty())
- {
- int parent=stk.top();
- stk.pop();
- for(int i=0;i<adj[parent].size();i++)
- {
- int child=adj[parent][i];
- if(visited[child]==0)
- {
- stk.push(parent);
- stk.push(child);
- visited[child]=1;
- cout<<child<<" ";
- break;
- }
- }
- }
- }
- int main()
- {
- int node,edge,m,n;
- cin>>node>>edge;
- while(edge--)
- {
- cin>>m>>n;
- adj[m].push_back(n);
- adj[n].push_back(m);
- }
- dfs(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement