Advertisement
Korotkodul

Задача B. Мосты и компоненты

Oct 20th, 2021
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 KB | None | 0 0
  1. #include <vector>
  2. #include <cmath>
  3. #include <algorithm>
  4. #include <set>
  5. #include <map>
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. map < pair <int, int>, bool> is_br;
  10. map <pair <int, int>, int> is_mult;
  11. void find_bridges(int v, int last, int self_depth, vector <vector<int>>& gr, vector <int>& depth, vector <int>& min_depth, vector <int>& color) {
  12.     if (color[v] == 2) return;
  13.     int self_min_depth = self_depth;
  14.     depth[v] = self_depth;
  15.     min_depth[v] = self_min_depth;
  16.     color[v] = 1;
  17.     for (int u : gr[v]) {
  18.         if (color[u] == 0) {
  19.             find_bridges(u, v, self_depth + 1, gr, depth, min_depth, color);//, bridges, edges
  20.         }
  21.         pair <int, int> ed1 = { min(v,u), max(v,u) };
  22.         pair <int, int> ed2 = { max(v,u), min(v,u) };
  23.         if (u != last || (u == last && is_mult[{ min(v, u), max(v, u) }] > 1)) {
  24.             self_min_depth = min(self_min_depth, min_depth[u]);
  25.             min_depth[v] = self_min_depth;
  26.             if (depth[v] < min_depth[u]) {
  27.                 is_br[{ min(v, u), max(v, u) }] = true;
  28.             }
  29.         }
  30.     }
  31.     color[v] = 2;
  32. }
  33.  
  34.  
  35. void components(int v, vector <vector<int>>& gr, vector <int>& color, vector <pair<int, int>>& bridges, vector <int>& This) {
  36.     if (color[v] == 2) return;
  37.     color[v] = 1;
  38.     This.push_back(v);
  39.     for (int u : gr[v]) {
  40.         //проверяем, что это рёбро не мост
  41.         if (is_br[{ min(v, u), max(v, u) }] == true) continue;
  42.         if (color[u] == 0) {
  43.             components(u, gr, color, bridges, This);
  44.         }
  45.     }
  46.     color[v] = 2;
  47. }
  48.  
  49. int main()
  50. {
  51.     ios_base::sync_with_stdio(false);
  52.     cin.tie(0);
  53.     int n, m;
  54.     cin >> n >> m;
  55.     vector <vector<int>> gr(n + 1);
  56.     for (int i = 1; i < m + 1; ++i) {
  57.         int a, b;
  58.         cin >> a >> b;
  59.         gr[a].push_back(b);
  60.         gr[b].push_back(a);
  61.         is_mult[{ min(a, b), max(a, b) }]++;
  62.     }
  63.     vector <int> color(n + 1, 0);
  64.     vector <pair<int, int>> bridges;
  65.     vector <int> depth(n + 1, 1e9);
  66.     vector <int> min_depth(n + 1, 1e9);
  67.     for (int i = 1; i <= n; ++i) {
  68.         if (color[i] != 0) continue;
  69.         find_bridges(i, i, 0, gr, depth, min_depth, color);
  70.     }
  71.     color.assign(n + 1, 0);
  72.     vector <int> This;
  73.     vector <vector <int> > answer;
  74.     for (int i = 1; i <= n; ++i) {
  75.         if (color[i] != 0) continue;
  76.         This.resize(0);
  77.         components(i, gr, color, bridges, This);
  78.         if (This.empty()) continue;
  79.         sort(This.begin(), This.end());
  80.         answer.push_back(This);
  81.     }
  82.     int num = answer.size();
  83.     cout << num << '\n';
  84.     for (auto x : answer) {
  85.         for (auto y : x) cout << y << ' ';
  86.         cout << '\n';
  87.     }cout << '\n';
  88. }  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement