Advertisement
El_GEMMY

endless walk

Apr 6th, 2022
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.00 KB | None | 0 0
  1. // Those who cannot remember the past are
  2. // condemned to repeat it (use DP -_-)
  3. // - George Santayana
  4.  
  5. #include <bits/stdc++.h>
  6. #include <ext/pb_ds/assoc_container.hpp>
  7. #include <ext/pb_ds/tree_policy.hpp>
  8.  
  9. using namespace std;
  10. using namespace __gnu_pbds;
  11.  
  12. #define all(v) v.begin(),v.end()
  13. #define rall(v) v.rbegin(),v.rend()
  14. #define ll long long
  15. #define ull unsigned long long
  16. #define MOD 1000000007
  17. #define PI 3.14159265
  18. #define ceil(a, b) ((a / b) + (a % b ? 1 : 0))
  19. #define imin INT_MIN
  20. #define imax INT_MAX
  21. #define inf 2000000000
  22. #define nl '\n'
  23. #define ppcnt __builtin_popcount
  24. #define ppcntll __builtin_popcountll
  25. #define clz __builtin_clz
  26. #define clzll __builtin_clzll
  27. #define ctz __builtin_ctz
  28. #define ctzll __builtin_ctzll
  29. #define modulo(a, b, mod) ((((a) % mod) * ((b) % mod)) % mod)
  30. #define cnte(v, x) count(all(v), (x))
  31. #define mine(v) min_element(all(v))
  32. #define maxe(v) max_element(all(v))
  33. #define debug(x) cout << "x: " << x << nl;
  34. #define debug2(x, y) cout << "x: " << x << " y: " << y << nl;
  35. #define ordered_set tree<pair<int, int>, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update>
  36. #define ordered_map tree<int, int, less<>, rb_tree_tag, tree_order_statistics_node_update>
  37.  
  38. //vector<int> dx = {0, 0, 1, -1, 1, 1, -1, -1}, dy = {1, -1, 0, 0, 1, -1, 1, -1};
  39. //vector<int> dx = {0, 0, 1, -1}, dy = {1, -1, 0, 0};
  40.  
  41. template<typename T = int> istream& operator>>(istream& in, vector<pair<int, int>>& v){
  42.     for (auto& [x, y] : v) in >> x >> y;
  43.     return in;
  44. }
  45.  
  46. template<typename T = int> istream& operator>>(istream& in, vector<T>& v){
  47.     for (T& i : v) in >> i;
  48.     return in;
  49. }
  50.  
  51. template<typename T = int> ostream& operator<<(ostream& out, const vector<T>& v){
  52.     for (const T& x : v)
  53.         out << x << ' ';
  54.     return out;
  55. }
  56.  
  57. template<typename T = pair<int, int>> ostream& operator << (ostream& out, const vector<pair<int, int>>& v){
  58.     for(auto& [x, y] : v){
  59.         out << x << ' ' << y << nl;
  60.     }
  61.     return out;
  62. }
  63.  
  64. void Start_Crushing() {
  65.     ios_base::sync_with_stdio(false);
  66.     cin.tie(nullptr);
  67.     cout.tie(nullptr);
  68. #ifndef ONLINE_JUDGE
  69.     freopen("input.txt", "r", stdin);
  70.     freopen("output.txt", "w", stdout);
  71. #endif
  72. }
  73.  
  74. vector<vector<int>> adj, rev;
  75. vector<bool> vis;
  76. vector<int> order;
  77. set<int> component;
  78. vector<set<int>> components;
  79.  
  80. void dfs1(int src){
  81.     vis[src] = true;
  82.  
  83.     for(auto& nxt : adj[src]){
  84.         if(not vis[nxt])
  85.             dfs1(nxt);
  86.     }
  87.     order.emplace_back(src);
  88. }
  89.  
  90. void dfs2(int src){
  91.     vis[src] = true;
  92.     component.insert(src);
  93.  
  94.     for(auto& nxt : rev[src]){
  95.         if(not vis[nxt])
  96.             dfs2(nxt);
  97.     }
  98. }
  99.  
  100. void solve(){
  101.     int n, m; cin >> n >> m;
  102.     adj.resize(n + 1), rev.resize(n + 1);
  103.  
  104.     while(m--){
  105.         int u, v; cin >> u >> v;
  106.         adj[u].emplace_back(v);
  107.         rev[v].emplace_back(u);
  108.     }
  109.  
  110.     vis.assign(n + 1, false);
  111.  
  112.     for(int i = 1; i <= n; i++)
  113.         if(not vis[i])
  114.             dfs1(i);
  115.  
  116.  
  117.     vis.assign(n + 1, false);
  118.     reverse(all(order));
  119.  
  120.     for(auto& i : order)
  121.         if(not vis[i]){
  122.             dfs2(i);
  123.  
  124.             if(component.size() > 1)
  125.                 components.emplace_back(component);
  126.  
  127.             component.clear();
  128.         }
  129.  
  130.     vis.assign(n + 1, false);
  131.  
  132.     for(auto& c : components){
  133.         for(auto& i : c)
  134.             vis[i] = true;
  135.     }
  136.  
  137.     int ans = 0;
  138.     while(not order.empty()){
  139.         for(auto& i : adj[order.back()]){
  140.             if(vis[i]){
  141.                 ans++;
  142.                 vis[order.back()] = true;
  143.                 break;
  144.             }
  145.         }
  146.         order.pop_back();
  147.     }
  148.     cout << ans;
  149. }
  150.  
  151. int main(){
  152.     Start_Crushing();
  153.  
  154.     int t = 1;
  155. //    /*Multiple test cases?*/ cin >> t;
  156.     while (t--) {
  157.         solve();
  158.         if(!t) break;
  159.         cout << nl;
  160.     }
  161.  
  162. //    for(int tc = 1; tc <= t; tc++){
  163. //        cout << "Case #" << tc << ": ";
  164. //        solve();
  165. //        if(tc != t)
  166. //            cout << nl;
  167. //    }
  168.  
  169.     return 0;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement