Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. #include <iostream>
  2. #include<algorithm>
  3. #include <vector>
  4.  
  5. struct Component
  6. {
  7. std::vector<int> component_vertices;
  8.  
  9. Component(std::vector<int> arr_of_component_vertices)
  10. {
  11. for (int i = 0; i < arr_of_component_vertices.size(); i++)
  12. {
  13. component_vertices.push_back(arr_of_component_vertices[i]);
  14. }
  15. }
  16.  
  17. };
  18.  
  19. class Resolver
  20. {
  21. int V;
  22. int E;
  23. std::vector<std::vector<int>> arr;
  24. std::vector<std::vector<int>> arr_transp;
  25. std::vector<Component> arr_of_components;
  26. std::vector<bool> used;
  27. std::vector<int> order;
  28. std::vector<int> components;
  29.  
  30. std::vector<std::vector<bool>> table_of_components_graph;
  31.  
  32. std::vector<int> component_of_verticle;
  33.  
  34. public:
  35. Resolver(int _V, int _E)
  36. {
  37. V = _V;
  38. E = _E;
  39. arr.resize(V, std::vector<int>());
  40. arr_transp.resize(V, std::vector<int>());
  41. used.resize(V);
  42. component_of_verticle.resize(V);
  43. }
  44.  
  45. void AddEdgeToGraph(int v, int u)
  46. {
  47. arr[v].push_back(u);
  48. }
  49.  
  50.  
  51. void AddEdgeToTranspGraph(int v, int u)
  52. {
  53. arr_transp[v].push_back(u);
  54. }
  55.  
  56.  
  57. void FirstDFS(int v)
  58. {
  59. used[v] = true;
  60. for (int i = 0; i < arr[v].size(); i++)
  61. {
  62. if (!used[arr[v][i]])
  63. {
  64. FirstDFS(arr[v][i]);
  65. }
  66. }
  67.  
  68. order.push_back(v);
  69. }
  70.  
  71.  
  72. void SecondDFS(int v)
  73. {
  74. used[v] = true;
  75. components.push_back(v);
  76.  
  77. for (int i = 0; i < arr_transp[v].size(); i++)
  78. {
  79. if (!used[arr_transp[v][i]])
  80. {
  81. SecondDFS(arr_transp[v][i]);
  82. }
  83. }
  84. }
  85.  
  86.  
  87. int GetAnswer()
  88. {
  89. for (int i = 0; i < V; i++)
  90. {
  91. if (!used[i])
  92. {
  93. FirstDFS(i);
  94. }
  95. }
  96.  
  97.  
  98. for (int i = 0; i < V; i++)
  99. {
  100. used[i] = false;
  101. }
  102.  
  103.  
  104. for (int i = 0; i < V; i++)
  105. {
  106. int cur = order[V - i - 1];
  107. if (!used[cur])
  108. {
  109. SecondDFS(cur);
  110.  
  111. arr_of_components.push_back(components);
  112.  
  113. components.clear();
  114. }
  115. }
  116.  
  117. if (arr_of_components.size() == 1)
  118. {
  119. return 0;
  120. }
  121.  
  122.  
  123. for (int i = 0; i < arr.size(); i++)
  124. {
  125. //находим, какой компоненте связности принадлежит данная вершина
  126. int cur_comp;
  127.  
  128. for (int k = 0; k < arr_of_components.size(); k++)
  129. {
  130. if (std::find(arr_of_components[k].component_vertices.begin(), arr_of_components[k].component_vertices.end(), i) != arr_of_components[k].component_vertices.end())
  131. {
  132. component_of_verticle[i] = k;
  133. break;
  134. }
  135. }
  136. }
  137.  
  138. table_of_components_graph.resize(arr_of_components.size(), std::vector<bool>(arr_of_components.size()));
  139.  
  140.  
  141. //заполняем таблицу смежности сконденсированного графа
  142.  
  143. for (int i = 0; i < arr.size(); i++)
  144. {
  145. for (int j = 0; j < arr[i].size(); j++)
  146. {
  147. if (component_of_verticle[i] != component_of_verticle[arr[i][j]])
  148. {
  149. table_of_components_graph[component_of_verticle[i]][component_of_verticle[arr[i][j]]] = true;
  150. }
  151. }
  152. }
  153.  
  154.  
  155.  
  156. int X = 0; //out
  157. int Y = 0; //in
  158.  
  159.  
  160. for (int i = 0; i < table_of_components_graph.size(); i++)
  161. {
  162.  
  163. int cur_out = 0;
  164. for (int j = 0; j < table_of_components_graph.size(); j++)
  165. {
  166. if (table_of_components_graph[i][j] == true)
  167. {
  168. cur_out++;
  169. break;
  170. }
  171. }
  172.  
  173. if (cur_out == 0)
  174. {
  175. X++;
  176. }
  177.  
  178. }
  179.  
  180.  
  181. for (int i = 0; i < table_of_components_graph.size(); i++)
  182. {
  183. int cur_in = 0;
  184. for (int j = 0; j < table_of_components_graph.size(); j++)
  185. {
  186. if (table_of_components_graph[j][i] == true)
  187. {
  188. cur_in++;
  189. break;
  190. }
  191. }
  192.  
  193. if (cur_in == 0)
  194. {
  195. Y++;
  196. }
  197. }
  198.  
  199. return std::max(X, Y);
  200. }
  201.  
  202. };
  203.  
  204. int main()
  205. {
  206.  
  207. int V;
  208. int E;
  209. std::cin >> V;
  210. std::cin >> E;
  211.  
  212. Resolver resolver(V, E);
  213. for (int i = 0; i < E; i++)
  214. {
  215. int u;
  216. int v;
  217. std::cin >> u >> v;
  218. resolver.AddEdgeToGraph(u - 1, v - 1);
  219. resolver.AddEdgeToTranspGraph(v - 1, u - 1);
  220. }
  221.  
  222. std::cout << resolver.GetAnswer();
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement