結果

問題 No.2652 [Cherry 6th Tune N] Δρονε χιρχλινγ
ユーザー t98slidert98slider
提出日時 2024-03-05 00:19:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 678 ms / 2,000 ms
コード長 3,949 bytes
コンパイル時間 4,174 ms
コンパイル使用メモリ 233,968 KB
実行使用メモリ 51,168 KB
最終ジャッジ日時 2024-03-05 00:20:43
合計ジャッジ時間 48,455 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 341 ms
6,676 KB
testcase_02 AC 341 ms
6,676 KB
testcase_03 AC 352 ms
6,676 KB
testcase_04 AC 390 ms
6,676 KB
testcase_05 AC 398 ms
6,676 KB
testcase_06 AC 399 ms
6,676 KB
testcase_07 AC 465 ms
16,628 KB
testcase_08 AC 473 ms
15,060 KB
testcase_09 AC 514 ms
17,572 KB
testcase_10 AC 476 ms
18,016 KB
testcase_11 AC 481 ms
16,800 KB
testcase_12 AC 501 ms
17,668 KB
testcase_13 AC 587 ms
24,156 KB
testcase_14 AC 597 ms
30,092 KB
testcase_15 AC 527 ms
20,416 KB
testcase_16 AC 478 ms
17,168 KB
testcase_17 AC 535 ms
18,448 KB
testcase_18 AC 551 ms
19,688 KB
testcase_19 AC 526 ms
23,404 KB
testcase_20 AC 646 ms
32,436 KB
testcase_21 AC 614 ms
32,180 KB
testcase_22 AC 633 ms
32,180 KB
testcase_23 AC 612 ms
32,180 KB
testcase_24 AC 639 ms
32,052 KB
testcase_25 AC 613 ms
32,308 KB
testcase_26 AC 619 ms
32,052 KB
testcase_27 AC 627 ms
32,180 KB
testcase_28 AC 615 ms
32,180 KB
testcase_29 AC 605 ms
32,180 KB
testcase_30 AC 600 ms
32,308 KB
testcase_31 AC 617 ms
32,308 KB
testcase_32 AC 623 ms
31,924 KB
testcase_33 AC 595 ms
32,436 KB
testcase_34 AC 678 ms
32,180 KB
testcase_35 AC 600 ms
32,308 KB
testcase_36 AC 600 ms
32,052 KB
testcase_37 AC 617 ms
32,052 KB
testcase_38 AC 593 ms
32,180 KB
testcase_39 AC 579 ms
32,180 KB
testcase_40 AC 318 ms
51,168 KB
testcase_41 AC 332 ms
41,524 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct dsu {
    public:
    int csz;
    dsu() : _n(0) {}
    dsu(int n) : _n(n), csz(n), parent_or_size(n, -1) {}

    int merge(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        int x = leader(a), y = leader(b);
        if (x == y) return x;
        if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
        csz--;
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        return x;
    }

    bool same(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        return leader(a) == leader(b);
    }

    int leader(int a) {
        assert(0 <= a && a < _n);
        if (parent_or_size[a] < 0) return a;
        return parent_or_size[a] = leader(parent_or_size[a]);
    }

    int size(int a) {
        assert(0 <= a && a < _n);
        return -parent_or_size[leader(a)];
    }

    std::vector<std::vector<int>> groups() {
        std::vector<int> leader_buf(_n), group_size(_n);
        for (int i = 0; i < _n; i++) {
            leader_buf[i] = leader(i);
            group_size[leader_buf[i]]++;
        }
        std::vector<std::vector<int>> result(_n);
        for (int i = 0; i < _n; i++) {
            result[i].reserve(group_size[i]);
        }
        for (int i = 0; i < _n; i++) {
            result[leader_buf[i]].push_back(i);
        }
        result.erase(
            std::remove_if(result.begin(), result.end(),
                           [&](const std::vector<int>& v) { return v.empty(); }),
            result.end());
        return result;
    }

    private:
    int _n;
    // root node: -1 * component size
    // otherwise: parent
    std::vector<int> parent_or_size;
};

vector<tuple<int,int,int>> manhattan_mst(vector<int> xs, vector<int> ys) {
    assert(xs.size() == ys.size());
    vector<tuple<int,int,int>> ret;
    int n = (int) xs.size();

    vector< int > ord(n);
    iota(ord.begin(), ord.end(), 0);

    for(int s = 0; s < 2; s++) {
        for(int t = 0; t < 2; t++) {
        auto cmp = [&](int i, int j) -> bool {
            return xs[i] + ys[i] < xs[j] + ys[j];
        };
        sort(ord.begin(), ord.end(), cmp);

        map< int, int > idx;
        for(int i:ord) {
            for(auto it = idx.lower_bound(-ys[i]);
                it != idx.end(); it = idx.erase(it)) {
            int j = it->second;
            if(xs[i] - xs[j] < ys[i] - ys[j]) break;
            ret.emplace_back(i, j, abs(xs[i] - xs[j]) + abs(ys[i] - ys[j]));
            }
            idx[-ys[i]] = i;
        }
        swap(xs, ys);
        }
        for(int i = 0; i < n; i++) xs[i] *= -1;
    }
    return ret;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    cin >> T;
    while(T--){
        int N, L;
        cin >> N >> L;
        vector<int> x(N), y(N);
        for(int i = 0; i < N; i++){
            cin >> x[i] >> y[i];
        }
        dsu uf(N);
        auto edge = manhattan_mst(x, y);
        sort(edge.begin(), edge.end(), [&](tuple<int,int,int> lhs, tuple<int,int,int> rhs){
            return get<2>(lhs) < get<2>(rhs);
        });
        vector<vector<int>> g(N);
        for(auto [u, v, c] : edge){
            if(uf.same(u, v)) continue;
            uf.merge(u, v);
            g[u].push_back(v);
            g[v].push_back(u);
        }
        vector<pair<int,int>> ans;
        auto dfs = [&](auto dfs, int v, int p) -> void {
            ans.emplace_back(x[v], y[v]);
            for(auto u : g[v]){
                if(u == p) continue;
                dfs(dfs, u, v);
            }
        };
        dfs(dfs, 0, -1);
        ll d = 0;
        for(int i = 0; i + 1 < N; i++){
            d += abs(ans[i].first - ans[i + 1].first) + abs(ans[i].second - ans[i + 1].second);
        }
        cout << N << '\n';
        for(auto [x, y] : ans) cout << x << ' ' << y << '\n';
    }
}
0