SHARE
TWEET

DFS

shabbyheart Jun 16th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. vector<int>adj[100];
  4. bool visited[100];
  5. void dfs(int src)
  6. {
  7.     visited[src]=true;
  8.     cout<<src<<" ";
  9.     for(int i=0;i<adj[src][i];i++)
  10.     {
  11.         int child=adj[src][i];
  12.         if(visited[child]==0)
  13.         {
  14.             dfs(child);
  15.         }
  16.     }
  17.  
  18. }
  19. int main()
  20. {
  21.     int node,edge,m,n;
  22.     cin>>node>>edge;
  23.     while(edge--)
  24.     {
  25.         cin>>m>>n;
  26.         adj[m].push_back(n);
  27.         adj[n].push_back(m);
  28.     }
  29.     dfs(1);
  30. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top