Advertisement
Josif_tepe

Untitled

Oct 17th, 2023
1,076
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <stack>
  4. using namespace std;
  5. const int maxn = 1e5 + 10;
  6. vector<int> graph[maxn];
  7. int discovarable_time[maxn];
  8. int low_link[maxn];
  9. bool on_stack[maxn];
  10. int current_time = 0;
  11. stack<int> st;
  12. int n;
  13. int cnt = 0;
  14. int res=  0;
  15. void dfs(int node) {
  16.     discovarable_time[node] = current_time;
  17.     low_link[node] = current_time;
  18.     current_time++;
  19.     st.push(node);
  20.     on_stack[node] = true;
  21.    
  22.     for(int i = 0; i < (int) graph[node].size(); i++) {
  23.         int neighbour = graph[node][i];
  24.         if(discovarable_time[neighbour] == -1) {
  25.             dfs(neighbour);
  26.             low_link[node] = min(low_link[node], low_link[neighbour]);
  27.         }
  28.         else if(on_stack[neighbour]) {
  29.             low_link[node] = min(low_link[node], discovarable_time[neighbour]);
  30.         }
  31.     }
  32.    
  33.     if(discovarable_time[node] == low_link[node]) {
  34.         cnt++;
  35.         int tmp_cnt = 0;
  36.         while(!st.empty()) {
  37.             int tmp = st.top();
  38.             st.pop();
  39.             on_stack[tmp] = false;
  40.             tmp_cnt++;
  41.             res = max(res, tmp_cnt);
  42.  
  43.             if(tmp == node) {
  44.                 break;
  45.             }
  46.         }
  47.        
  48.     }
  49. }
  50. void tarjan() {
  51.     for(int i = 0; i < maxn; i++) {
  52.         discovarable_time[i] = -1;
  53.         on_stack[i] = false;
  54.     }
  55.     for(int i = 0; i < n; i++) {
  56.         if(discovarable_time[i] == -1) {
  57.             dfs(i);
  58.         }
  59.     }
  60. }
  61. int main() {
  62.     int  m;
  63.     cin >> n >> m;
  64.    
  65.     for(int i = 0; i < m; i++) {
  66.         int a, b;
  67.         cin >> a >> b;
  68.         a--; b--;
  69.         graph[a].push_back(b);
  70.     }
  71.     tarjan();
  72.     cout << cnt << endl << res << endl;
  73.     return 0;
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement