結果

問題 No.1390 Get together
ユーザー sten_sansten_san
提出日時 2021-02-12 21:52:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 163 ms / 2,000 ms
コード長 1,351 bytes
コンパイル時間 2,475 ms
コンパイル使用メモリ 204,960 KB
実行使用メモリ 12,608 KB
最終ジャッジ日時 2023-09-27 03:45:24
合計ジャッジ時間 6,241 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 3 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 4 ms
4,376 KB
testcase_05 AC 5 ms
4,380 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 4 ms
4,380 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 5 ms
4,424 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 3 ms
4,452 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 110 ms
9,552 KB
testcase_17 AC 150 ms
12,428 KB
testcase_18 AC 117 ms
9,816 KB
testcase_19 AC 159 ms
12,396 KB
testcase_20 AC 157 ms
12,424 KB
testcase_21 AC 162 ms
12,544 KB
testcase_22 AC 139 ms
11,732 KB
testcase_23 AC 152 ms
11,412 KB
testcase_24 AC 141 ms
11,376 KB
testcase_25 AC 159 ms
12,608 KB
testcase_26 AC 162 ms
12,564 KB
testcase_27 AC 163 ms
12,428 KB
testcase_28 AC 158 ms
12,432 KB
testcase_29 AC 159 ms
12,496 KB
testcase_30 AC 157 ms
12,352 KB
testcase_31 AC 158 ms
12,408 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct uns_t {} uns;
template <typename Element, typename Head, typename ...Args>
auto vec(Element init, Head arg, Args ...args) {
    if constexpr (sizeof...(Args) == 0) return std::vector(arg, init);
    else return std::vector(arg, vec(init, args...));
}
template <typename Element, typename Head, typename ...Args>
auto vec(uns_t, Head arg, Args ...args) {
    return vec(Element(), arg, args...);
}

int par[200000 + 1];

void init() {
    fill(begin(par), end(par), -1);
}

int root(int x) {
    if (par[x] < 0) {
        return x;
    }
    return par[x] = root(par[x]);
}

void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) return;
    par[x] += par[y];
    par[y] = x;
}

bool same(int x, int y) {
    return root(x) == root(y);
}

int size(int x) {
    return -par[root(x)];
}

int main() {
    init();

    int n, m; cin >> n >> m;
    auto a = vec<int>(uns, n, 0);
    for (int i = 0; i < n; ++i) {
        int b, c; cin >> b >> c; --b; --c;
        a[c].push_back(b);
    }

    for (auto &b : a) {
        for (int i = 1; i < b.size(); ++i) {
            unite(b[i - 1], b[i]);
        }
    }

    int ans = 0;
    for (int i = 0; i < m; ++i) {
        if (same(i, m)) continue;
        ans += size(i) - 1;
        unite(i, m);
    }

    cout << ans << endl;
}

0