Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <algorithm>
  5. #include <stack>
  6.  
  7. using namespace std;
  8.  
  9. vector<int> used;
  10. vector< vector< pair < int, int > > > g;
  11.  
  12. int timer;
  13. int maxColour = -1;
  14. vector<int> tin;
  15. vector<int> up;
  16.  
  17. vector<int> colours;
  18.  
  19.  
  20.  
  21. void push(int v, int colour, int parent) {
  22.  
  23. used[v] = 1;
  24.  
  25. for (size_t i = 0; i < g[v].size(); ++i) {
  26. int to = g[v][i].first;
  27. int num = g[v][i].second;
  28.  
  29. if (to == parent)
  30. continue;
  31.  
  32. if (!used[to]) {
  33. if (up[to] >= tin[v]) {
  34. int t = ++maxColour;
  35. colours[num] = t;
  36. push(to, t, v);
  37.  
  38. }
  39. else {
  40. colours[num] = colour;
  41. push(to, colour, v);
  42. }
  43.  
  44. }
  45. else if (tin[to] < tin[v])
  46. colours[num] = colour;
  47. }
  48.  
  49. }
  50.  
  51.  
  52.  
  53. int dfs(int v, int parent) {
  54.  
  55. used[v] = 1;
  56. tin[v] = up[v] = timer++;
  57.  
  58. for (size_t i = 0; i < g[v].size(); ++i) {
  59. int to = g[v][i].first;
  60.  
  61. if (to == parent)
  62. continue;
  63.  
  64. if (used[to])
  65. up[v] = min(up[v], tin[to]);
  66.  
  67. else {
  68.  
  69. dfs(to, v);
  70. up[v] = min(up[v], up[to]);
  71.  
  72. }
  73. }
  74.  
  75. return 0;
  76. }
  77.  
  78. int doSomething() {
  79.  
  80. for (size_t v = 0; v < g.size(); ++v) {
  81. dfs(v, -1);
  82. }
  83.  
  84. used.assign(used.size(), 0);
  85. maxColour = 0;
  86. for (size_t v = 0; v < g.size(); ++v) {
  87. if (!used[v]) {
  88. maxColour++;
  89. push(v, maxColour, -1);
  90. }
  91. }
  92.  
  93. return 0;
  94. }
  95.  
  96. int main() {
  97.  
  98. int n = 0, m = 0, cnt = 0;
  99. cin >> n >> m;
  100. timer = 0;
  101.  
  102. g.resize(n);
  103. used.resize(n, 0);
  104. tin.resize(n, 0);
  105. up.resize(n, 0);
  106. colours.resize(m);
  107.  
  108. for (size_t i = 0; i < m; ++i) {
  109. int p = 0, q = 0;
  110. cin >> p >> q;
  111.  
  112. p--;
  113. q--;
  114.  
  115. g[p].push_back({ q, i });
  116. g[q].push_back({ p, i });
  117. }
  118.  
  119. doSomething();
  120.  
  121.  
  122. cout << maxColour - 1 << endl;
  123. for (int i = 0; i < colours.size(); i++)
  124. cout << colours[i] - 1 << " ";
  125.  
  126. cin >> n;
  127. return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement