Advertisement
istinishat

DFS

Jun 3rd, 2016
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <queue>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. #define si(a) scanf("%d",&a)
  10. #define f first
  11. #define s second
  12. #define mp(a,b) make_pair(a,b)
  13. #define MAX 1005
  14.  
  15. int vis[MAX];
  16. vector<int> graph[MAX];
  17.  
  18. void dfs(int now)
  19. {
  20.     if(vis[now])
  21.         return ;
  22.     //cout<<now+1<<endl;
  23.     vis[now]=1;
  24.     for(int i=0;i<graph[now].size();i++)
  25.         dfs(graph[now][i]);
  26.     return ;
  27. }
  28.  
  29. int main()
  30. {
  31.     //freopen("input","r",stdin);
  32.     int n,m,i;
  33.     si(n);si(m);
  34.     for(i=0;i<m;i++){
  35.         int u,v;
  36.         si(u);si(v);
  37.         u--;v--;
  38.         graph[u].push_back(v);
  39.         graph[v].push_back(u);
  40.     }
  41.     dfs(0);
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement