Advertisement
Josif_tepe

Untitled

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