Dmaxiya

[蓝桥杯 2026 省 B] 应急布线 参考代码

Apr 11th, 2026
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define endl '\n'
  5.  
  6. typedef long long LL;
  7. const int maxn = 100000 + 100;
  8. struct DSU {
  9.     vector<int> parent;
  10.  
  11.     DSU(int n) {
  12.         parent.resize(n + 1);
  13.         for (int i = 0; i <= n; i++) {
  14.             parent[i] = i;
  15.         }
  16.     }
  17.  
  18.     int find(int x) {
  19.         return parent[x] == x ? x : parent[x] = find(parent[x]);
  20.     }
  21.  
  22.     void unite(int x, int y) {
  23.         x = find(x);
  24.         y = find(y);
  25.         if (x != y) {
  26.             parent[y] = x;
  27.         }
  28.     }
  29.  
  30.     bool same(int x, int y) {
  31.         return find(x) == find(y);
  32.     }
  33. };
  34.  
  35. int main() {
  36.     ios::sync_with_stdio(false);
  37.    
  38.     int n, m;
  39.     cin >> n >> m;
  40.    
  41.     int a, b;
  42.     int cnt = n;
  43.     DSU dsu(n);
  44.     for (int i = 0; i < m; ++i) {
  45.         cin >> a >> b;
  46.         if (!dsu.same(a, b)) {
  47.             dsu.unite(a, b);
  48.             --cnt;
  49.         }
  50.     }
  51.  
  52.     --cnt;
  53.     cout << cnt << " " << (cnt * 2 + n - 1) / n << endl;
  54.    
  55.     return 0;
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment