Salvens

E

Aug 12th, 2023
599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.22 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <array>
  4. #include <vector>
  5. #include <numeric>
  6. #include <random>
  7. #include <chrono>
  8. #include <set>
  9. #include <map>
  10. #include <queue>
  11.  
  12. #define int long long
  13. using namespace std;
  14.  
  15. const long long INF = 1e9 + 7;
  16. const int MAXN = 1e5 + 1000;
  17. const int N = 1e5 + 10;
  18.  
  19. vector<vector<pair<int, int>>> g;
  20. vector<vector<int>> tree;
  21. array<int, MAXN> used, tin, up, color;
  22. int timer = 0;
  23. set<int> bridge;
  24. map<int, pair<int, int>> decompress;
  25.  
  26. void dfs(int v, int e = -1) {
  27.     used[v] = true;
  28.     tin[v] = up[v] = timer++;
  29.     for (auto [to, num]: g[v]) {
  30.         if (num == e) {
  31.             continue;
  32.         }
  33.         if (used[to]) {
  34.             up[v] = min(up[v], tin[to]);
  35.         } else {
  36.             dfs(to, num);
  37.             up[v] = min(up[v], up[to]);
  38.             if (up[to] > tin[v]) {
  39.                 bridge.insert(num);
  40.             }
  41.         }
  42.     }
  43. }
  44.  
  45. int col = 0;
  46.  
  47. void paint(int v) {
  48.     used[v] = true;
  49.     color[v] = col;
  50.     for (auto [to, num]: g[v]) {
  51.         if (bridge.contains(num)) {
  52.             continue;
  53.         }
  54.         if (!used[to]) {
  55.             paint(to);
  56.         }
  57.     }
  58. }
  59.  
  60. inline void solve() {
  61.     int n, m;
  62.     cin >> n >> m;
  63.     g.clear();
  64.     g.resize(n);
  65.     for (int i = 0; i < m; ++i) {
  66.         int u, v;
  67.         cin >> u >> v;
  68.         --u, --v;
  69.         g[u].emplace_back(v, i);
  70.         g[v].emplace_back(u, i);
  71.         decompress[i] = {u, v};
  72.     }
  73.  
  74.     used.fill(0); // нахождение мостов
  75.     tin.fill(0);
  76.     up.fill(0);
  77.     bridge.clear();
  78.     timer = 0;
  79.     for (int i = 0; i < n; ++i) {
  80.         if (!used[i]) {
  81.             dfs(i);
  82.         }
  83.     }
  84.  
  85.     used.fill(0); // раскраска компонент без мостов
  86.     color.fill(0);
  87.     col = 0;
  88.     for (int i = 0; i < n; ++i) {
  89.         if (!used[i]) {
  90.             paint(i);
  91.             ++col;
  92.         }
  93.     }
  94.  
  95.     tree.clear(); // построение дерева
  96.     tree.resize(n);
  97.     for (auto& i: bridge) {
  98.         auto [u, v] = decompress[i];
  99.         u = color[u];
  100.         v = color[v];
  101.         tree[u].emplace_back(v);
  102.         tree[v].emplace_back(u);
  103.     }
  104.  
  105.     vector<int> d(col, -1);
  106.     queue<int> q;
  107.     d[0] = 0;
  108.     q.push(0);
  109.     int max_dist = 0;
  110.     while (!q.empty()) {
  111.         int v = q.front();
  112.         q.pop();
  113.         for (auto to: tree[v]) {
  114.             if (d[to] == -1) {
  115.                 d[to] = d[v] + 1;
  116.                 q.push(to);
  117.             }
  118.             if (d[to] > d[max_dist]) {
  119.                 max_dist = to;
  120.             }
  121.         }
  122.     }
  123.  
  124.     fill(d.begin(), d.end(), -1);
  125.     int start = max_dist;
  126.     d[start] = 0;
  127.     q.push(start);
  128.     while (!q.empty()) {
  129.         int v = q.front();
  130.         q.pop();
  131.         for (auto to: tree[v]) {
  132.             if (d[to] == -1) {
  133.                 d[to] = d[v] + 1;
  134.                 q.push(to);
  135.             }
  136.             if (d[to] > d[max_dist]) {
  137.                 max_dist = to;
  138.             }
  139.         }
  140.     }
  141.  
  142.     cout << bridge.size() - d[max_dist] << '\n';
  143. }
  144.  
  145. signed main() {
  146.     ios_base::sync_with_stdio(false);
  147.     cin.tie(nullptr);
  148.  
  149.     int tt = 1;
  150.     cin >> tt;
  151.     while (tt--) {
  152.         solve();
  153.     }
  154. }
  155.  
Advertisement
Add Comment
Please, Sign In to add comment