結果

問題 No.1767 BLUE to RED
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-05-28 21:12:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 327 ms / 2,000 ms
コード長 1,674 bytes
コンパイル時間 1,186 ms
コンパイル使用メモリ 123,048 KB
実行使用メモリ 33,128 KB
最終ジャッジ日時 2023-08-27 16:25:50
合計ジャッジ時間 8,761 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 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 177 ms
17,816 KB
testcase_10 AC 231 ms
29,596 KB
testcase_11 AC 187 ms
18,680 KB
testcase_12 AC 176 ms
17,640 KB
testcase_13 AC 232 ms
30,272 KB
testcase_14 AC 308 ms
31,860 KB
testcase_15 AC 317 ms
32,500 KB
testcase_16 AC 309 ms
31,396 KB
testcase_17 AC 311 ms
31,216 KB
testcase_18 AC 318 ms
31,352 KB
testcase_19 AC 317 ms
31,848 KB
testcase_20 AC 325 ms
31,520 KB
testcase_21 AC 317 ms
32,772 KB
testcase_22 AC 327 ms
33,128 KB
testcase_23 AC 313 ms
31,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <numeric>
#include <deque>

using namespace std;
using ll = long long;
 
struct UnionFind {
    vector<int> par;
    vector<int> siz;
 
    UnionFind(int N) : par(N), siz(N) {
        for(int i = 0; i < N; i++){
            par[i] = i;
            siz[i] = 1;
        }
    }
 
    int root(int x) {
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }
 
    void unite(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        if (rx == ry) return;
        par[rx] = ry;
        siz[ry] += siz[rx];
    }
 
    bool same(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
 
    int size(int x){
        return siz[root(x)];
    }
};

template<typename T> using pq = priority_queue<T, vector<T>, greater<T>>;
 
int main(){
 
    ll N, M, A, B, c, x, y, ans=0;
    cin >> N >> M;
    vector<pair<ll, ll>> X;
    for (int i=0; i<N; i++){
        cin >> A;
        X.push_back({A, 0});
    }
    for (int i=0; i<M; i++){
        cin >> B;
        X.push_back({B, 1});
    }
    sort(X.begin(), X.end());
    pq<tuple<ll, ll, ll>> que;
    UnionFind tree(N+M+1);


    for (int i=0; i<N+M; i++){
        if (i) que.push({X[i].first-X[i-1].first, i, i-1});
        if (X[i].second == 0) tree.unite(i, N+M);
    }

    while(!que.empty()){
        tie(c, x, y) = que.top();
        que.pop();
        if (!tree.same(x, y)){
            ans += c;
            tree.unite(x, y);
        }
    }
 
    cout << ans << endl;
 
    return 0;
}
0