Advertisement
Josif_tepe

Untitled

Dec 12th, 2022
684
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 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. int discoverable_time[maxn];
  10. int low_link[maxn];
  11. bool on_stack[maxn];
  12. int current_time = 0;
  13. vector<vector<int> > scc;
  14. void dfs(int node) {
  15.     discoverable_time[node] = current_time;
  16.     low_link[node] = current_time;
  17.     current_time++;
  18.     on_stack[node] = true;
  19.    
  20.     static stack<int> st;
  21.     st.push(node);
  22.    
  23.     for(int i = 0; i < (int) graph[node].size(); i++) {
  24.         int neighbour = graph[node][i];
  25.         if(discoverable_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], discoverable_time[neighbour]);
  31.         }
  32.     }
  33.     if(low_link[node] == discoverable_time[node]) {
  34.         vector<int> v;
  35.         while(!st.empty()) {
  36.             int t = st.top();
  37.             st.pop();
  38.             on_stack[t] = false;
  39.             v.push_back(t);
  40.             if(t == node) {
  41.                 break;
  42.             }
  43.         }
  44.         scc.push_back(v);
  45.     }
  46. }
  47. void tarjanSCC() {
  48.     for(int i = 0; i < maxn; i++) {
  49.         discoverable_time[i] = -1;
  50.         on_stack[i] = false;
  51.         low_link[i] = -1;
  52.     }
  53.     for(int i = 0; i < n; i++) {
  54.         if(discoverable_time[i] == -1) {
  55.             dfs(i);
  56.         }
  57.     }
  58. }
  59. int main()
  60. {
  61.     cin >> n >> m;
  62.    
  63.     for(int i = 0; i < m; i++) {
  64.         int a, b;
  65.         cin >> a >> b;
  66.         a--; b--;
  67.         graph[a].push_back(b);
  68.     }
  69.     tarjanSCC();
  70.     cout << scc.size() << endl;
  71.     int result = 0;
  72.     for(int i = 0; i < scc.size(); i++) {
  73.         result = max(result, (int)scc[i].size());
  74.     }
  75.     cout << result << endl;
  76.     return 0;
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement