Advertisement
Icemore

Untitled

Jun 19th, 2014
1,107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.54 KB | None | 0 0
  1. int n;
  2. int graph[10000][10000];
  3.  
  4. void dfs(int start) {
  5.     stack<pair<int, int>> st;
  6.     vector<int> used(n);
  7.    
  8.     st.push(make_pair(start, 0));
  9.     used[start] = 1;
  10.  
  11.     while (!st.empty()) {
  12.         if (st.top().second == 0) {
  13.             cout << st.top().first << endl;
  14.         }
  15.    
  16.         for (pair<int, int> & cur = st.top(); cur.second < n; ++cur.second) {
  17.             if (graph[cur.first][cur.second] && !used[cur.second]) {
  18.                 st.push(make_pair(cur.second, 0));
  19.                 used[cur.second] = 1;
  20.                 break;
  21.             }
  22.         }
  23.  
  24.         if (st.top().second == n) {
  25.             st.pop();
  26.         }
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement