Advertisement
GerONSo

5 ЛКШ

Apr 11th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. // #define pragma
  2.  
  3. #ifdef pragma
  4. #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
  5. // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  6. #endif // pragma
  7.  
  8. #include<bits/stdc++.h>
  9. #include <ext/pb_ds/assoc_container.hpp>
  10. #include <ext/pb_ds/tree_policy.hpp>
  11.  
  12. #define ll long long
  13. #define all(x) begin(x), end(x)
  14. #define pb push_back
  15. #define x first
  16. #define y second
  17. #define int long long
  18. #define zero(two) memset(two, 0, sizeof(two))
  19.  
  20. using namespace std;
  21. using namespace __gnu_pbds;
  22.  
  23.  
  24. typedef vector<int> vi;
  25. typedef vector<bool> vb;
  26. typedef pair<int, int> pii;
  27. typedef long double ld;
  28. typedef vector<vi> matrix;
  29. template<typename T>
  30. using kawaii_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  31.  
  32. const ld PI = atan2(0, -1);
  33.  
  34. void seriy() {
  35. ios::sync_with_stdio(0);
  36. cin.tie(0);
  37. cout.tie(0);
  38. // cout << fixed << setprecision(10);
  39. #if 0
  40. freopen("input", "r", stdin);
  41. freopen("output", "w", stdout);
  42. #endif
  43. }
  44.  
  45. const int MAXN = 2e5 + 10;
  46. const int INF = 1e15 + 7;
  47. const int MAXLOG = 31;
  48. const int MOD = 998244353;
  49. const int BASE = 47;
  50.  
  51. matrix g, gt;
  52. vi tp;
  53. vb used;
  54. vi col(MAXN);
  55. vi t(MAXN);
  56.  
  57. void dfs(int u) {
  58. used[u] = 1;
  59. for(auto v : g[u]) {
  60. if(!used[v]) {
  61. dfs(v);
  62. }
  63. }
  64. tp.pb(u);
  65. }
  66.  
  67. void dfs2(int u, int cur) {
  68. t[cur]++;
  69. col[u] = cur;
  70. for(auto v : gt[u]) {
  71. if(!col[v]) {
  72. dfs2(v, cur);
  73. }
  74. }
  75. }
  76.  
  77. signed main() {
  78. seriy();
  79. int n, m;
  80. cin >> n >> m;
  81. // assert(n != 2 && m != 2);
  82. g.resize(n);
  83. gt.resize(n);
  84. used.resize(n);
  85. vector<pii> edges;
  86. for(int i = 0; i < m; i++) {
  87. int u, v;
  88. cin >> u >> v;
  89. u--;
  90. v--;
  91. edges.pb({u, v});
  92. g[u].pb(v);
  93. gt[v].pb(u);
  94. }
  95. for(int i = 0; i < n; i++) {
  96. if(!used[i]) {
  97. dfs(i);
  98. }
  99. }
  100.  
  101. reverse(all(tp));
  102. int cur = 1;
  103. for(int i = 0; i < n; i++) {
  104. if(!col[tp[i]]) {
  105. dfs2(tp[i], cur);
  106. cur++;
  107. }
  108. }
  109. int cnt = 0;
  110. map<pii, bool> mp;
  111. for(auto i : edges) {
  112. if(col[i.x] == col[i.y]) {
  113. cnt++;
  114. cnt -= mp[{i.x, i.y}];
  115. mp[{i.x, i.y}] = 1;
  116. }
  117. }
  118. int ans = 0;
  119. for(int i = 0; i < t.size(); i++) {
  120. ans += t[i] * (t[i] - 1);
  121. }
  122.  
  123. cout << ans - cnt;
  124. return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement