Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <map>
  6. #include <set>
  7.  
  8. using namespace std;
  9.  
  10. int MAXN = 200001;
  11. int n, m;
  12. int cur_color = 1;
  13. int t;
  14. vector<vector<pair<int, int>>> g(MAXN);
  15. vector<bool> used(MAXN);
  16. vector<int> ans;
  17. vector<int> color(MAXN);
  18. vector<int> tin(MAXN);
  19. vector<int> up(MAXN);
  20.  
  21. void fillgraph();
  22.  
  23. void points_search();
  24.  
  25. void dfs_points(int v, int p);
  26.  
  27. void fillgraph() {
  28.     t = 0;
  29.     cin >> n >> m;
  30.     for (int i = 0; i < n; i++) {
  31.         used[i] = false;
  32.     }
  33.     for (int i = 0; i < m; i++) {
  34.         int a, b;
  35.         cin >> a >> b;
  36.         a--;
  37.         b--;
  38. //        if (a > b) {
  39. //            std::swap(a, b);
  40. //        }
  41.         bool flag = false;
  42. //        for (int j = 0; j < g[a].size(); j++) {
  43. //            if (g[a][j] == b) {
  44. //                flag = true;
  45. //            }
  46. //        }
  47. //        for (int j = 0; j < g[b].size(); j++) {
  48. //            if (g[b][j] == a) {
  49. //                flag = true;
  50. //            }
  51. //        }
  52.         if (!flag) {
  53.             g[b].emplace_back(a, i);
  54.             g[a].emplace_back(b, i);
  55.         }
  56.     }
  57. }
  58.  
  59. void points_search() {
  60.     for (int i = 0; i < n; i++) {
  61.         if (!used[i]) {
  62.             dfs_points(i, -1);
  63.         }
  64.     }
  65. }
  66.  
  67. void dfs_points(int v, int p) {
  68.     used[v] = true;
  69.     tin[v] = t;
  70.     up[v] = t;
  71.     t++;
  72.     int ch = 0;
  73.     for (int i = 0; i < g[v].size(); i++) {
  74.         int to = g[v][i].first;
  75.         if (to != p) {
  76.             if (!used[to]) {
  77.                 dfs_points(to, v);
  78.                 up[v] = min(up[v], up[to]);
  79.                 if (up[to] >= tin[v]) {
  80.                     if (p >= 0) {
  81.                         ans.push_back(v);
  82.                     }
  83.                     ch++;
  84.                 }
  85.             } else {
  86.                 up[v] = min(up[v], tin[to]);
  87.             }
  88.         }
  89.     }
  90.     if (p == -1) {
  91.         if (ch > 1) {
  92.             ans.push_back(v);
  93.         }
  94.     }
  95. }
  96.  
  97. void vert(int v, int col, int p) {
  98.     {
  99.         int i = v;
  100.         used[v] = true;
  101.         for (int j = 0; j < g[i].size(); j++) {
  102.             int to = g[v][j].first;
  103.             if (p != to) {
  104.                 if (used[to] == false) {
  105.                     if (tin[v] <= up[to]) {
  106.                         int cur = ++cur_color;
  107.                         color[g[v][j].second] = cur;
  108.                         vert(to, cur, v);
  109.                     } else {
  110.                         color[g[v][j].second] = col;
  111.                         vert(to, col, v);
  112.                     }
  113.                 } else if (tin[v] > tin[to]) {
  114.                     color[g[v][j].second] = col;
  115.                 }
  116.             }
  117.         }
  118.     }
  119. }
  120.  
  121. void comp_vert() {
  122.     for (int i = 0; i < n; i++) {
  123.         if (used[i] == false) {
  124.             cur_color++;
  125.             vert(i, cur_color, -1);
  126.         }
  127.     }
  128. }
  129.  
  130. int main() {
  131.     fillgraph();
  132.     for (int i = 0; i < n; i++) {
  133.         used[i] = false;
  134.     }
  135.     points_search();
  136.     for (int i = 0; i < n; i++) {
  137.         used[i] = false;
  138.     }
  139.     comp_vert();
  140.     set<int> different;
  141.     for (int i = 0; i < m; i++) {
  142.         different.insert(color[i]);
  143.     }
  144.     cout << different.size() << '\n';
  145.     vector<int> answer;
  146.     map<int, int> colorMap;
  147.     int index = 1;
  148.     for (auto el : different) {
  149.         colorMap[el] = index;
  150.         index++;
  151.     }
  152.     for (int i = 0; i < m; i++) {
  153.         answer.push_back((*colorMap.find(color[i])).second);
  154.     }
  155.     for (int i = 0; i < answer.size(); i++) {
  156.         cout << answer[i] << " ";
  157.     }
  158.     cout << '\n';
  159.     return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement