結果

問題 No.2072 Anatomy
ユーザー shoshoshomshoshoshom
提出日時 2022-09-16 22:36:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,573 bytes
コンパイル時間 2,078 ms
コンパイル使用メモリ 206,912 KB
実行使用メモリ 9,872 KB
最終ジャッジ日時 2023-08-23 16:11:24
合計ジャッジ時間 4,619 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 42 ms
7,828 KB
testcase_25 WA -
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 42 ms
7,780 KB
testcase_28 WA -
権限があれば一括ダウンロードができます

ソースコード

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