結果

問題 No.2072 Anatomy
ユーザー shoshoshomshoshoshom
提出日時 2022-09-16 22:36:52
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,573 bytes
コンパイル時間 1,877 ms
コンパイル使用メモリ 200,656 KB
最終ジャッジ日時 2025-02-07 10:09:21
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 3 WA * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

class dsu {
  vector<int> p;
  vector<int> sz;
  vector<int> edges;

public:
  dsu(int n) : p(n), sz(n, 1), edges(n), groups(0) {
    iota(p.begin(), p.end(), 0);
  }

  int groups;
  inline int get(int x) {
    if (x == p[x]) {
      return x;
    }
    return p[x] = get(p[x]);
  }

  inline bool unite(int x, int y) {
    x = get(x);
    y = get(y);
    if (x != y) {
      if (sz[x] < sz[y]) {
        swap(x, y);
      }
      if (sz[x] > 1 && sz[y] > 1) {
        groups--;
      } else if (sz[x] == 1 && sz[y] == 1) {
        groups++;
      }
      p[y] = x;
      sz[x] += sz[y];
      return true;
    }
    return false;
  }

  inline int size(int x) {
    x = get(x);
    return sz[x];
  }
};

int main() {
  iostream::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> A(m);
  for (auto &p : A) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    p = {x, y};
  }

  reverse(A.begin(), A.end());

  dsu uf(n + 1);
  vector<int> groups(m);
  for (int i = 0; i < m; i++) {
    uf.unite(A[i].first, A[i].second);
    groups[i] = uf.groups;
  }
  reverse(groups.begin(), groups.end());
  vector<pair<int, int>> rl;
  for (int g : groups) {
    if (rl.empty() || rl.back().first != g) {
      rl.push_back({g, 1});
    } else {
      rl.back().second++;
    }
  }
  int ans = 0;
  for (int i = 0; i < (int)rl.size();) {
    ans += rl[i].second;
    if (rl[i].second > 1) {
      i++; 
    } else {
      i += rl[i].first;
    }
  }
  cout << ans << '\n';

  return 0;
}
0