Advertisement
wurdalak007

Untitled

Feb 20th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. //
  2. // main.cpp
  3. // дополнение до сильносвязного
  4. //
  5. // Created by Матвей on 19.02.2018.
  6. // Copyright © 2018 Матвей. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <vector>
  11.  
  12. using namespace std;
  13.  
  14. class Graph {
  15. public:
  16. Graph( int n );
  17. void AddEdge( int from, int to );
  18. void first_dfs(int v);
  19. void sec_dfs(int v);
  20. void find_comp();
  21. void fill_which_comp();
  22. int find_stock();
  23.  
  24. private:
  25. vector<vector<int>> graph;
  26. vector<vector<int>> t_graph;
  27. vector<bool> used;
  28. vector<int> order, component;
  29. vector<int> which_comp;
  30. vector< vector<int> > all_components;
  31. };
  32.  
  33. Graph::Graph( int n ): graph(n), t_graph(n), used(n, false), which_comp(n) {}
  34.  
  35. int Graph::find_stock() {
  36. int stock = 0;
  37. int not_stock = 0;
  38. find_comp();
  39. fill_which_comp();
  40.  
  41. for( int i = 0; i < all_components.size(); i++ ) {
  42. for( int j : all_components[i] ) {
  43. for( int vert : graph[j] ) {
  44. if( which_comp[vert] != i )
  45. stock++;
  46. }
  47. }
  48. }
  49.  
  50. for( int i = 0; i < all_components.size(); i++ ) {
  51. for( int j : all_components[i] ) {
  52. for( int vert : t_graph[j] ) {
  53. if( which_comp[vert] != i )
  54. not_stock++;
  55. }
  56. }
  57. }
  58.  
  59. return max(stock, not_stock);
  60. }
  61.  
  62. void Graph::fill_which_comp() {
  63. for( int i = 0; i < all_components.size(); i++ ){
  64. for( int j : all_components[i] ) {
  65. which_comp[j] = i;
  66. }
  67. }
  68. }
  69.  
  70. void Graph::AddEdge( int from, int to ) {
  71. graph[from].push_back(to);
  72. t_graph[to].push_back(from);
  73. }
  74.  
  75. void Graph::first_dfs( int v ) {
  76. used[v] = true;
  77. for( int i = 0; i < graph[v].size(); i++ )
  78. if( !used[ graph[v][i] ] )
  79. first_dfs( graph[v][i] );
  80. order.push_back(v);
  81. }
  82.  
  83. void Graph::sec_dfs( int v ) {
  84. used[v] = true;
  85. component.push_back (v);
  86. for( int i = 0; i < t_graph[v].size(); i++ )
  87. if( !used[ t_graph[v][i] ] )
  88. sec_dfs( t_graph[v][i] );
  89. }
  90.  
  91. void Graph::find_comp() {
  92. int n = (int)graph.size();
  93. used.assign (n, false);
  94.  
  95. for( int i = 0; i < n; i++ )
  96. if( !used[i] )
  97. first_dfs(i);
  98.  
  99. used.assign (n, false);
  100. for( int i = 0; i < n; i++ ) {
  101. int v = order[n-1-i];
  102. if( !used[v] ) {
  103. sec_dfs(v);
  104. all_components.push_back(component);
  105. component.clear();
  106. }
  107. }
  108. }
  109.  
  110. int main() {
  111. int vertex = 0;
  112. int edges = 0;
  113. cin >> vertex >> edges;
  114.  
  115. Graph graph(vertex);
  116. for( int i = 0; i < edges; i++ ) {
  117. int from,to;
  118. cin >> from >> to;
  119. graph.AddEdge(from-1, to-1);
  120. }
  121. cout << graph.find_stock();
  122.  
  123. return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement