結果

問題 No.1390 Get together
ユーザー yansi819
提出日時 2024-05-01 12:31:04
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 417 ms / 2,000 ms
コード長 617 bytes
コンパイル時間 4,554 ms
コンパイル使用メモリ 257,472 KB
最終ジャッジ日時 2025-02-21 10:01:41
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
using ld = long double;
using mint = modint998244353;

int main() {
  int N, M; cin >> N >> M;
  vector<set<int>> cnt(N);
  dsu uf(M);

  for (int i = 0; i < N; i++) {
    int b, c; cin >> b >> c;
    b--, c--;
    cnt[c].insert(b);
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    set<int> s;
    for (auto v: cnt[i]) {
       s.insert(uf.leader(v));
    }
    ans += max(0, (int)s.size() - 1);
    for (auto v: s) {
      uf.merge(*s.begin(), v);
    }
  }
  cout << ans << endl;
  return 0;
}
0