Advertisement
shabbyheart

DFS

Jun 16th, 2019
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement