結果

問題 No.1390 Get together
ユーザー m_tsubasam_tsubasa
提出日時 2021-02-12 21:27:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 123 ms / 2,000 ms
コード長 950 bytes
コンパイル時間 2,297 ms
コンパイル使用メモリ 202,244 KB
実行使用メモリ 4,888 KB
最終ジャッジ日時 2023-09-27 02:48:50
合計ジャッジ時間 6,095 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 3 ms
4,384 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 4 ms
4,380 KB
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,384 KB
testcase_13 AC 2 ms
4,384 KB
testcase_14 AC 1 ms
4,384 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 107 ms
4,556 KB
testcase_17 AC 111 ms
4,624 KB
testcase_18 AC 116 ms
4,708 KB
testcase_19 AC 121 ms
4,648 KB
testcase_20 AC 120 ms
4,676 KB
testcase_21 AC 123 ms
4,648 KB
testcase_22 AC 117 ms
4,652 KB
testcase_23 AC 123 ms
4,716 KB
testcase_24 AC 122 ms
4,592 KB
testcase_25 AC 122 ms
4,620 KB
testcase_26 AC 122 ms
4,888 KB
testcase_27 AC 121 ms
4,624 KB
testcase_28 AC 122 ms
4,684 KB
testcase_29 AC 123 ms
4,716 KB
testcase_30 AC 123 ms
4,556 KB
testcase_31 AC 122 ms
4,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct Unionfind {
  // tree number
  vector<int> par;
  // constructor
  Unionfind(int n = 1) : par(n, -1) {}
  // search root
  int root(int x) {
    if (par[x] < 0) return x;
    return par[x] = root(par[x]);
  }
  // is same?
  bool issame(int x, int y) { return root(x) == root(y); }

  // add
  // already added, return 0
  bool uni(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) return 0;
    if (par[x] > par[y]) swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return 1;
  }
  int size(int x) { return -par[root(x)]; }
};

int n, m;
Unionfind uf;
vector<int> v;

int main() {
  cin >> n >> m;
  uf = Unionfind(m);
  v.assign(n, -1);
  int res = 0;
  for (int i = 0; i < n; ++i) {
    int b, c;
    cin >> b >> c;
    --b, --c;
    if (v[c] >= 0) {
      if (!uf.issame(b, v[c])) ++res, uf.uni(b, v[c]);
    } else
      v[c] = b;
  }
  cout << res << endl;
  return 0;
}
0