結果

問題 No.1390 Get together
ユーザー ninoinuininoinui
提出日時 2021-02-12 23:03:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 68 ms / 2,000 ms
コード長 1,862 bytes
コンパイル時間 2,255 ms
コンパイル使用メモリ 209,112 KB
実行使用メモリ 16,860 KB
最終ジャッジ日時 2023-09-27 06:16:36
合計ジャッジ時間 5,123 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 44 ms
14,044 KB
testcase_17 AC 62 ms
16,676 KB
testcase_18 AC 45 ms
14,040 KB
testcase_19 AC 68 ms
16,648 KB
testcase_20 AC 66 ms
16,628 KB
testcase_21 AC 68 ms
16,680 KB
testcase_22 AC 57 ms
15,672 KB
testcase_23 AC 58 ms
15,688 KB
testcase_24 AC 58 ms
15,596 KB
testcase_25 AC 67 ms
16,860 KB
testcase_26 AC 68 ms
16,584 KB
testcase_27 AC 66 ms
16,584 KB
testcase_28 AC 66 ms
16,736 KB
testcase_29 AC 67 ms
16,576 KB
testcase_30 AC 68 ms
16,636 KB
testcase_31 AC 64 ms
16,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <class A, class B> bool cmin(A& a, B b) { return a > b && (a = b, true); }
template <class A, class B> bool cmax(A& a, B b) { return a < b && (a = b, true); }

class UnionFind {
private:
  long N;
  vector<long> PS;

public:
  UnionFind() : N(0) {}
  UnionFind(long n) : N(n), PS(n, -1) {}
  bool unite(long a, long b) {
    long x = root(a), y = root(b);
    if (x == y) return false;
    if (-PS.at(x) < -PS.at(y)) swap(x, y);
    PS.at(x) += PS.at(y);
    PS.at(y) = x;
    return true;
  }
  bool same(long a, long b) { return root(a) == root(b); }
  long root(long a) {
    if (PS.at(a) < 0) return a;
    return PS.at(a) = root(PS.at(a));
  }
  long size(long a) { return -PS.at(root(a)); }
  vector<vector<long>> groups() {
    vector<long> B(N), S(N);
    for (long i = 0; i < N; i++) {
      B.at(i) = root(i);
      S.at(B.at(i))++;
    }
    vector<vector<long>> ret(N);
    for (long i = 0; i < N; i++) {
      ret.at(i).reserve(S.at(i));
    }
    for (long i = 0; i < N; i++) {
      ret.at(B.at(i)).push_back(i);
    }
    auto f = [&](const vector<long> &v) { return v.empty(); };
    ret.erase(remove_if(ret.begin(), ret.end(), f), ret.end());
    return ret;
  }
};

signed main() {
  cin.tie(nullptr)->sync_with_stdio(false);

  long N, M;
  cin >> N >> M;
  vector<pair<long, long>> V(N);
  for (long i = 0; i < N; i++) {
    long a, b;
    cin >> a >> b, a--, b--;
    V.at(i) = {a, b};
  }

  vector<vector<long>> G(N);
  for (const auto& [a, b] : V) G.at(b).push_back(a);

  long ans = 0;
  UnionFind UF(M);
  for (const auto& g : G) {
    if ((long)g.size() < 2) continue;
    long n = (long)g.size();
    long f = g.front();
    for (long i = 1; i < n; i++) {
      if (!UF.same(f, g.at(i))) {
        UF.unite(f, g.at(i));
        ans++;
      }
    }
  }
  cout << ans << '\n';
}
0