Advertisement
Korotkodul

ЗОШ конденсация OK

Jan 9th, 2022 (edited)
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #include <set>
  9. #include <map>
  10. #define pii pair <int,int>
  11. using namespace std;
  12. using ll = long long;
  13. using ld = long double;
  14. void cv(vector <int> &v){
  15. for (auto x: v) cout<<x<<' ';
  16. cout<<"\n";
  17. }
  18.  
  19. void cvl(vector <ll> &v){
  20. for (auto x: v) cout<<x<<' ';
  21. cout<<"\n";
  22. }
  23.  
  24.  
  25. void cvv(vector <vector <int> > &v){
  26. for (auto x: v) cv(x);
  27. cout<<"\n";
  28. }
  29. int n,m;
  30. vector <vector<int>>G;
  31. vector <vector<int>> reG;
  32. const int wh = 0, gr = 1, bl = 2;
  33. vector <int> clr;
  34. vector <int> tp;
  35.  
  36.  
  37. bool paint = 0;
  38. int cmp_num = 0;
  39.  
  40. vector <int> comp;
  41.  
  42. void dfs(int v, int pr){
  43.  
  44. clr[v] = gr;
  45. if (paint) comp[v] = cmp_num;
  46. for (int u: G[v]){
  47. if (u == pr) continue;
  48. else if (clr[u] == wh){
  49. dfs(u, v);
  50. }
  51.  
  52. }
  53. clr[v] = bl;
  54. if (!paint) tp.push_back(v);
  55. }
  56.  
  57. //void re
  58.  
  59.  
  60. int main()
  61. {
  62. ios::sync_with_stdio(0);
  63. cin.tie(0);
  64. cout.tie(0);
  65. cin>>n>>m;
  66. G.resize(n);
  67. reG.resize(n);
  68. clr.assign(n, wh);
  69. comp.resize(n,-1);
  70. for (int i=0;i<m;++i){
  71. int a,b;
  72. cin>>a>>b;
  73. a--; b--;
  74. G[a].push_back(b);
  75. reG[b].push_back(a);
  76. }
  77.  
  78. for (int i = 0;i<n;++i){
  79. if (clr[i] == wh){
  80. dfs(i, -1);
  81. }
  82. }
  83.  
  84. reverse(tp.begin(), tp.end());
  85.  
  86. paint = 1;
  87. G = reG;
  88. clr.assign(n, wh);
  89.  
  90. for (int i=0;i<n;++i){
  91.  
  92. if (clr[tp[i]] == wh){
  93. dfs(tp[i], -1);
  94. cmp_num++;
  95. }
  96. }
  97.  
  98. set <pii> ans;
  99. for (int i = 0;i<n;++i){
  100.  
  101. for (int j=0;j<G[i].size();++j){
  102.  
  103. if (comp[G[i][j]] != comp[i]) {
  104.  
  105. //ans.insert( {min(i,G[i][j]), max(i, G[i][j])} );
  106. ans.insert( { min( comp[i], comp[G[i][j]]) , max(comp[i], comp[G[i][j]]) } );
  107. }
  108.  
  109. }
  110. }
  111. cout<<ans.size()<<"\n";
  112. }
  113.  
  114. /*
  115. 8 11
  116. 1 2
  117. 2 1
  118. 2 3
  119. 3 4
  120. 4 5
  121. 5 3
  122. 5 6
  123. 6 4
  124. 7 8
  125. 8 7
  126. 7 1
  127.  
  128. 8 10
  129. 1 2
  130. 2 1
  131. 2 3
  132. 3 4
  133. 4 5
  134. 5 3
  135. 5 6
  136. 6 4
  137. 7 8
  138. 8 7
  139.  
  140. 4 4
  141. 2 1
  142. 3 2
  143. 2 3
  144. 4 3
  145.  
  146. 6 8
  147. 1 2
  148. 2 1
  149. 2 3
  150. 3 4
  151. 4 5
  152. 5 3
  153. 5 6
  154. 6 4
  155.  
  156.  
  157. */
  158.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement