結果

問題 No.1390 Get together
ユーザー merom686merom686
提出日時 2021-02-12 21:58:20
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 47 ms / 2,000 ms
コード長 1,083 bytes
コンパイル時間 788 ms
コンパイル使用メモリ 87,168 KB
実行使用メモリ 4,688 KB
最終ジャッジ日時 2023-09-27 03:54:48
合計ジャッジ時間 3,324 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 39 ms
4,564 KB
testcase_17 AC 40 ms
4,536 KB
testcase_18 AC 41 ms
4,564 KB
testcase_19 AC 46 ms
4,608 KB
testcase_20 AC 45 ms
4,572 KB
testcase_21 AC 46 ms
4,612 KB
testcase_22 AC 45 ms
4,544 KB
testcase_23 AC 46 ms
4,584 KB
testcase_24 AC 45 ms
4,544 KB
testcase_25 AC 46 ms
4,540 KB
testcase_26 AC 46 ms
4,656 KB
testcase_27 AC 46 ms
4,688 KB
testcase_28 AC 45 ms
4,560 KB
testcase_29 AC 47 ms
4,672 KB
testcase_30 AC 46 ms
4,572 KB
testcase_31 AC 47 ms
4,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

struct UnionFind {
    UnionFind(int n) : p(n, -1) {}
    int root(int u) {
        return p[u] < 0 ? u : p[u] = root(p[u]);
    }
    int size(int u) {
        return -p[root(u)];
    }
    bool same(int u, int v) {
        return root(u) == root(v);
    }
    void unite(int u, int v) {
        u = root(u);
        v = root(v);
        if (u == v) return;
        if (p[u] > p[v]) swap(u, v);
        p[u] += p[v];
        p[v] = u;
    }
    vector<int> p;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n, m;
    cin >> n >> m;

    UnionFind uf(m);
    vector<int> d(n, -1);
    int r = 0;

    for (int i = 0; i < n; i++) {
        int b, c;
        cin >> b >> c;
        b--; c--;

        int t = d[c];
        d[c] = b;
        if (t < 0) continue;
        if (uf.same(t, b)) continue;
        uf.unite(t, b);
        r++;
    }

    cout << r << endl;

    return 0;
}
0