Advertisement
Emiliatan

tanjan

Nov 24th, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. /* tanjan */
  2. #include <vector>
  3. #include <stack>
  4.  
  5. constexpr int _MAXV = 1e5;
  6.  
  7. vector<int> _G[_MAXV + 1];
  8. stack<int> _st;
  9. int _cnt, _dfn[_MAXV + 1]{}, _low[_MAXV + 1]{};
  10. bool _instack[_MAXV + 1]{};
  11.  
  12. void _tanjan(int now)
  13. {
  14.     if (_dfn[now]) return;
  15.  
  16.     _dfn[now] = _low[now] = ++_cnt;
  17.     _st.emplace(now);
  18.     _instack[now] = true;
  19.  
  20.     for (auto nex : _G[now])
  21.     {
  22.         if (!_dfn[nex])
  23.         {
  24.             _tanjan(nex);
  25.             _low[now] = min(_low[now], _low[nex]);
  26.         }
  27.         else if (_instack[nex])
  28.             _low[now] = min(_low[now], _dfn[nex]);
  29.     }
  30.  
  31.     if (_low[now] == _dfn[now])
  32.     {
  33.         int node;
  34.         do
  35.         {
  36.             node = _st.top(); _st.pop();
  37.             _instack[node] = false;
  38.  
  39.         } while (node != now);
  40.     }
  41. }
  42.  
  43. void init() noexcept
  44. {
  45.     for (int i = 1; i <= n; ++i)
  46.         _G[i].clear();
  47.  
  48.     _st = stack<int>();
  49.     memset(_instack, false, _MAXV + 1);
  50.     memset(_dfn, 0, sizeof(int) * (_MAXV + 1));
  51.     _cnt = 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement