Advertisement
tuki2501

tjalg.cpp

Nov 10th, 2021
1,115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 10005;
  5.  
  6. vector<int> adj[N], s;
  7. int num[N], low[N], timer = 0, cnt_ssc = 0;
  8.  
  9. void chmin(int &a, int b) {
  10.   if (a > b) a = b;
  11. }
  12.  
  13. void dfs(int i) {
  14.   num[i] = low[i] = ++timer;
  15.   s.push_back(i);
  16.   for (auto &j : adj[i]) {
  17.     if (!num[j]) {
  18.       dfs(j);
  19.       chmin(low[i], low[j]);
  20.     }
  21.     else {
  22.       chmin(low[i], num[j]);
  23.     }
  24.   }
  25.   if (num[i] == low[i]) {
  26.     cnt_ssc++;
  27.     while (true) {
  28.       int t = s.back(); s.pop_back();
  29.       num[t] = low[t] = INT_MAX;
  30.       if (t == i) break;
  31.     }
  32.   }
  33. }
  34.  
  35. int main() {
  36.   cin.tie(0)->sync_with_stdio(0);
  37.   int n, m;
  38.   cin >> n >> m;
  39.   for (int i = 1; i <= m; i++) {
  40.     int u, v;
  41.     cin >> u >> v;
  42.     adj[u].push_back(v);
  43.   }
  44.   for (int i = 1; i <= n; i++) {
  45.     if (!num[i]) dfs(i);
  46.   }
  47.   cout << cnt_ssc << '\n';
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement