Advertisement
Korotkodul

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

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