Advertisement
wurdalak007

Untitled

Feb 26th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. class My_Graph {
  7. public:
  8. My_Graph(int V, vector<vector<int>> g, vector<vector<int>> t) {
  9. graph.resize(V);
  10. trans.resize(V);
  11. used.resize(V, false);
  12. help.resize(V);
  13.  
  14. for (int i = 0; i < V; i++) {
  15. graph[i] = g[i];
  16. trans[i] = t[i];
  17. }
  18.  
  19. }
  20. ~My_Graph() = default;
  21.  
  22. void DFS_1(int vertex);
  23. void DFS_2(int vertex);
  24. void Build_Comp();
  25. int Get_Result();
  26. private:
  27. vector<vector<int>> graph;
  28. vector<vector<int>> trans;
  29. vector<bool> used;
  30. vector<int> order;
  31. vector<int> help;
  32. vector<vector<int>> components;
  33. vector<int> component;
  34. };
  35.  
  36. void My_Graph::DFS_1(int vertex) {
  37. used[vertex] = true;
  38. for (int i : graph[vertex]) {
  39. if (!used[i]) {
  40. DFS_1(i);
  41. }
  42. }
  43. order.push_back(vertex);
  44. }
  45.  
  46. void My_Graph::DFS_2(int vertex) {
  47. used[vertex] = true;
  48. component.push_back(vertex);
  49. for (int i : trans[vertex]) {
  50. if (!used[i]) {
  51. DFS_2(i);
  52. }
  53. }
  54. }
  55.  
  56. void My_Graph::Build_Comp() {
  57. used.assign(graph.size(), false);
  58. for (int i = 0; i < graph.size(); i++) {
  59. if (!used[i]) {
  60. DFS_1(i);
  61. }
  62. }
  63. used.assign(graph.size(), false);
  64. for (int i = 0; i < graph.size(); i++) {
  65. int temp = order[graph.size() - 1 - i];
  66. if (!used[temp]) {
  67. DFS_2(temp);
  68. for (int j : component) {
  69. help[j] = components.size();
  70. }
  71. components.push_back(component);
  72. component.clear();
  73. }
  74. }
  75. }
  76.  
  77. int My_Graph::Get_Result() {
  78. Build_Comp();
  79. int begs = 0;
  80. int ends = 0;
  81. bool fl = true;
  82.  
  83. for (int i = 0; i < components.size(); i++) {
  84. for (int v : components[i]) {
  85. for (int j : graph[v]) {
  86. if (help[j] != i) {
  87. fl = false;
  88. }
  89. }
  90. }
  91. if (fl) begs++;
  92. fl = true;
  93. }
  94.  
  95. for (int i = 0; i < components.size(); i++) {
  96. for (int v : components[i]) {
  97. for (int j : trans[v]) {
  98. if (help[j] != i) {
  99. fl = false;
  100. }
  101. }
  102. }
  103.  
  104. if (fl) ends++;
  105. fl = true;
  106.  
  107. }
  108.  
  109.  
  110.  
  111. int res = max(begs, ends);
  112. if (components.size() == 1) {
  113. res = 0;
  114. }
  115. return res;
  116. }
  117.  
  118. int main() {
  119. int V = 0;
  120. int E = 0;
  121. cin >> V >> E;
  122.  
  123. vector<vector<int>> graph;
  124. vector<vector<int>> trans;
  125. graph.resize(V);
  126. trans.resize(V);
  127.  
  128. int u = 0;
  129. int v = 0;
  130. for (int i = 0; i < E; i++) {
  131. cin >> u >> v;
  132. graph[u - 1].push_back(v - 1);
  133. trans[v - 1].push_back(u - 1);
  134. }
  135.  
  136. My_Graph Graph(V, graph, trans);
  137. cout << Graph.Get_Result();
  138. return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement