Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <map>
  6.  
  7. #define NIL -1
  8. using namespace std;
  9.  
  10. int components_count = 0;
  11. map <pair<int, int>, int> components;
  12.  
  13. class Graph {
  14. int V;
  15. int E;
  16. list <int> *adj;
  17. void dfs(int v, bool visited[], int up[], int low[],
  18. int parent[], list <pair<int, int > > *st);
  19.  
  20. public:
  21. Graph(int V);
  22. void addEdge(int v, int w);
  23. void find_components(vector<pair <int, int> > edges);
  24. };
  25.  
  26. Graph::Graph(int V) {
  27. this->V = V;
  28. this->E = 0;
  29. adj = new list <int >[V];
  30. }
  31.  
  32. void Graph::addEdge(int v, int w) {
  33. adj[v].push_back(w);
  34. adj[w].push_back(v);
  35. E++;
  36. }
  37.  
  38. void Graph::dfs(int u, bool visited[], int up[], int low[], int parent[], list <pair<int, int > > *st) {
  39. static int time = 0;
  40. visited[u] = true;
  41.  
  42. up[u] = low[u] = ++time;
  43.  
  44. int children = 0;
  45.  
  46. list<int >::iterator i;
  47. for (i = adj[u].begin(); i != adj[u].end(); ++i) {
  48. int v = *i;
  49.  
  50. if (!visited[v] && v != parent[v]) {
  51. children++;
  52. parent[v] = u;
  53. st->push_back(make_pair(u, v));
  54. dfs(v, visited, up, low, parent, st);
  55.  
  56. low[u] = min(low[u], low[v]);
  57. if ((parent[u] == NIL && children > 1) || (parent[u] != NIL && low[v] >= up[u])) {
  58. while (st->back().first != u || st->back().second != v) {
  59. components[make_pair(st->back().first, st->back().second)] = components_count;
  60. components[make_pair(st->back().second, st->back().first)] = components_count;
  61. st->pop_back();
  62. }
  63. components[make_pair(st->back().first, st->back().second)] = components_count;
  64. components[make_pair(st->back().second, st->back().first)] = components_count;
  65. st->pop_back();
  66.  
  67. components_count++;
  68. }
  69. } else if (v != parent[u]) {
  70. low[u] = min(low[u], up[v]);
  71. if (up[v] < up[u]) {
  72. st->push_back(make_pair(u, v));
  73. }
  74. }
  75. }
  76. }
  77.  
  78. void Graph::find_components(vector<pair <int, int> > edges) {
  79. bool *visited = new bool[V];
  80. int *up = new int[V];
  81. int *low = new int[V];
  82. int *parent = new int[V];
  83. list <pair<int, int > > *st = new list<pair<int, int > >[E];
  84.  
  85. for (int i = 0; i < V; i++) {
  86. parent[i] = NIL;
  87. visited[i] = false;
  88. }
  89.  
  90. for (int i = 0; i < V; i++) {
  91. if (visited[i] == false)
  92. dfs(i, visited, up, low, parent, st);
  93.  
  94. int j = 0;
  95. while (st->size() > 0) {
  96. j = 1;
  97. components[make_pair(st->back().first, st->back().second)] = components_count;
  98. components[make_pair(st->back().second, st->back().first)] = components_count;
  99. st->pop_back();
  100. }
  101. if (j == 1) {
  102. components_count++;
  103. }
  104. }
  105.  
  106. cout << components_count << endl;
  107.  
  108. for (pair<int,int> edge: edges) {
  109. cout << components[edge] + 1 << ' ';
  110. }
  111. }
  112.  
  113. int main() {
  114. int n, m;
  115.  
  116. cin >> n >> m;
  117. vector<pair <int, int> > edges;
  118. Graph g(n+1);
  119.  
  120. for (int i = 0; i < m; i++) {
  121. int from = 0;
  122. int to = 0;
  123. cin >> from >> to;
  124. g.addEdge(from, to);
  125. edges.push_back(make_pair(from, to));
  126. components[make_pair(from, to)] = NIL;
  127. }
  128.  
  129. g.find_components(edges);
  130.  
  131. return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement