답안 #965062

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
965062 2024-04-18T04:52:46 Z Pannda 자매 도시 (APIO20_swap) C++17
17 / 100
359 ms 524288 KB
#include "swap.h"

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

struct DSU {
    vector<int> f;
    vector<int> siz;
    vector<int> deg;
    vector<bool> ok;

    DSU() {}
    DSU(int n) : f(n), siz(n, 1), deg(n, 0), ok(n, false) { iota(f.begin(), f.end(), 0); }

    int leader(int u) {
        while (u != f[u]) u = f[u] = f[f[u]];
        return u;
    }

    bool unionize(int u0, int v0) {
        int u = leader(u0);
        int v = leader(v0);
        if (u == v) {
            ok[u] = true;
            return false;
        }
        if (siz[u] > siz[v]) swap(u, v);
        siz[v] += siz[u];
        if (++deg[u0] >= 3 || ++deg[v0] >= 3) ok[v] = true;
        ok[v] = ok[v] | ok[u];
        f[u] = v;
        return true;
    }

    bool same(int u, int v) {
        return leader(u) == leader(v);
    }

    bool isOk(int u) {
        return ok[leader(u)];
    }
};

struct LiteDSU {
    vector<int> f;
    vector<int> siz;
    vector<bool> ok;

    LiteDSU(int n) : f(n), siz(n, 1), ok(n, false) { iota(f.begin(), f.end(), 0); }

    int leader(int u) {
        while (u != f[u]) u = f[u] = f[f[u]];
        return u;
    }

    bool unionize(int u, int v) {
        u = leader(u);
        v = leader(v);
        if (u == v) return false;
        if (siz[u] > siz[v]) swap(u, v);
        siz[v] += siz[u];
        ok[v] = ok[v] | ok[u];
        f[u] = v;
        return true;
    }

    bool same(int u, int v) {
        return leader(u) == leader(v);
    }

    bool isOk(int u) {
        return ok[leader(u)];
    }
};

int n, m;
vector<array<int, 3>> edges;
const int B = 2;
DSU dsu;
struct Block {
    int c;
    vector<int> dom;
    vector<int> mp;
    DSU dsu;

    Block(int n, int b, DSU &dsu0, DSU &dsu1, vector<array<int, 3>> &edges) : mp(n, -1) {
        dsu = dsu0;
        for (int i = B * b; i < min(m, B * b + B); i++) {
            int u = dsu0.leader(edges[i][0]);
            int v = dsu0.leader(edges[i][1]);
            if (mp[u] == -1) mp[u] = 0, dom.push_back(u);
            if (mp[v] == -1) mp[v] = 0, dom.push_back(v);
        }
        c = dom.size();
        int label = 0;
        for (int u : dom) mp[u] = label++;
    }
};
vector<Block> blocks;

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
    n = N;
    m = M;
    edges.resize(m);
    for (int i = 0; i < m; i++) {
        edges[i] = {U[i], V[i], W[i]};
    }
    sort(edges.begin(), edges.end(), [&](array<int, 3> e0, array<int, 3> e1) { return e0[2] < e1[2]; });

    dsu = DSU(n);
    for (int b = 0; B * b < m; b++) {
        DSU next_dsu = dsu;
        for (int i = B * b; i < min(m, B * b + B); i++) {
            next_dsu.unionize(edges[i][0], edges[i][1]);
        }
        blocks.push_back(Block(n, b, dsu, next_dsu, edges));
        dsu = next_dsu;
    }
}

int getMinimumFuelCapacity(int X, int Y) {
    if (!dsu.same(X, Y) || !dsu.isOk(X)) return -1;
    int b = [&]() -> int {
        int l = 0, r = (int)blocks.size() - 1;
        while (l < r) {
            int m = (l + r + 1) >> 1;
            if (!blocks[m].dsu.same(X, Y) || !blocks[m].dsu.isOk(X)) {
                l = m;
            } else {
                r = m - 1;
            }
        }
        return l;
    }();
    int x = blocks[b].mp[blocks[b].dsu.leader(X)];
    int y = blocks[b].mp[blocks[b].dsu.leader(Y)];
    LiteDSU dsu(blocks[b].c);
    int res = -2;
    vector<int> defer;
    for (int u : blocks[b].dom) if (blocks[b].dsu.isOk(u)) dsu.ok[blocks[b].mp[u]] = true;
    for (int i = B * b; i < min(m, B * b + B); i++) {
        int u = blocks[b].mp[blocks[b].dsu.leader(edges[i][0])];
        int v = blocks[b].mp[blocks[b].dsu.leader(edges[i][1])];
        if (dsu.same(u, v)) {
            dsu.ok[dsu.leader(u)] = true;
        } else {
            defer.push_back(edges[i][0]);
            defer.push_back(edges[i][1]);
            if (++blocks[b].dsu.deg[edges[i][0]] >= 3) dsu.ok[dsu.leader(u)] = true;
            if (++blocks[b].dsu.deg[edges[i][1]] >= 3) dsu.ok[dsu.leader(v)] = true;
            dsu.unionize(u, v);
        }
        if (dsu.same(x, y) && dsu.isOk(x)) {
            res = edges[i][2];
            break;
        }
    }
    for (int u : defer) blocks[b].dsu.deg[u]--;
    return res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 2 ms 1884 KB Output is correct
5 Correct 3 ms 7516 KB Output is correct
6 Correct 3 ms 7004 KB Output is correct
7 Correct 4 ms 8284 KB Output is correct
8 Correct 4 ms 8284 KB Output is correct
9 Runtime error 359 ms 524288 KB Execution killed with signal 9
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Runtime error 319 ms 524288 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 2 ms 1884 KB Output is correct
5 Correct 3 ms 7516 KB Output is correct
6 Correct 3 ms 7004 KB Output is correct
7 Correct 4 ms 8284 KB Output is correct
8 Correct 4 ms 8284 KB Output is correct
9 Correct 1 ms 500 KB Output is correct
10 Correct 6 ms 8228 KB Output is correct
11 Correct 7 ms 8388 KB Output is correct
12 Correct 4 ms 8024 KB Output is correct
13 Correct 3 ms 5720 KB Output is correct
14 Correct 3 ms 6336 KB Output is correct
15 Correct 4 ms 8540 KB Output is correct
16 Correct 5 ms 8284 KB Output is correct
17 Correct 5 ms 8408 KB Output is correct
18 Correct 4 ms 8540 KB Output is correct
19 Correct 4 ms 5824 KB Output is correct
20 Correct 4 ms 8284 KB Output is correct
21 Correct 4 ms 8280 KB Output is correct
22 Correct 3 ms 3932 KB Output is correct
23 Correct 4 ms 5276 KB Output is correct
24 Correct 11 ms 14884 KB Output is correct
25 Correct 8 ms 15448 KB Output is correct
26 Correct 8 ms 16472 KB Output is correct
27 Correct 4 ms 8540 KB Output is correct
28 Correct 9 ms 15688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 500 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 2 ms 1884 KB Output is correct
6 Correct 3 ms 7516 KB Output is correct
7 Correct 3 ms 7004 KB Output is correct
8 Correct 4 ms 8284 KB Output is correct
9 Correct 4 ms 8284 KB Output is correct
10 Runtime error 359 ms 524288 KB Execution killed with signal 9
11 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 2 ms 1884 KB Output is correct
5 Correct 3 ms 7516 KB Output is correct
6 Correct 3 ms 7004 KB Output is correct
7 Correct 4 ms 8284 KB Output is correct
8 Correct 4 ms 8284 KB Output is correct
9 Runtime error 359 ms 524288 KB Execution killed with signal 9
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 500 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 2 ms 1884 KB Output is correct
6 Correct 3 ms 7516 KB Output is correct
7 Correct 3 ms 7004 KB Output is correct
8 Correct 4 ms 8284 KB Output is correct
9 Correct 4 ms 8284 KB Output is correct
10 Runtime error 359 ms 524288 KB Execution killed with signal 9
11 Halted 0 ms 0 KB -