Submission #714978

# Submission time Handle Problem Language Result Execution time Memory
714978 2023-03-25T15:46:45 Z dxz05 Swapping Cities (APIO20_swap) C++17
17 / 100
2000 ms 524288 KB
#pragma GCC optimize("Ofast,O3,unroll-loops")

#include "swap.h"
#include <bits/stdc++.h>

using namespace std;

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

const int MaxN = 100002;
const int MaxM = 100002;

int N, M;
const int blockLength = 450;

struct Set{
    int parent;
    bool line;
    pair<int, int> tail;
    Set(){};
    Set(int i){
        parent = i;
        line = true;
        tail = make_pair(i, i);
    }
};

Set t[MaxN];

int MAGIC;
bool flag[MaxN];
Set mem[MaxN][MaxM / blockLength + 3];

int re[MaxN];
int _nxt;

int leader(int x){
    if (MAGIC != -1 && !flag[x]){
        t[x] = (MAGIC == 0 ? Set(x) : mem[x][MAGIC - 1]);
        flag[x] = true;
        re[_nxt++] = x;
    }
    return t[x].parent == x ? x : t[x].parent = leader(t[x].parent);
}

void unite(int x, int y){
    int X = x, Y = y;
    x = leader(x);
    y = leader(y);

    if (rng() & 1){
        swap(x, y);
        swap(X, Y);
    }

    if (x == y){
        t[y].line = false;
        return;
    }

    t[x].parent = y;
    if (!t[x].line){
        t[y].line = false;
        return;
    }

    if (!t[y].line) return;

    if (t[x].tail.first != X && t[x].tail.second != X){
        t[y].line = false;
        return;
    }

    if (t[y].tail.first != Y && t[y].tail.second != Y){
        t[y].line = false;
        return;
    }

    pair<int, int> new_tail;
    new_tail.first = t[x].tail.first ^ t[x].tail.second ^ X;
    new_tail.second = t[y].tail.first ^ t[y].tail.second ^ Y;

    t[y].tail = new_tail;
}

int el[MaxM], er[MaxM];

int blockCount;

tuple<int, int, int> edges[MaxM];
void init(int _N, int _M, vector<int> U, vector<int> V, vector<int> W) {
    N = _N, M = _M;

    for (int i = 0; i < M; i++){
        edges[i] = make_tuple(W[i], U[i], V[i]);
    }

    sort(edges, edges + M);

    for (int i = 0; i < N; i++) t[i] = Set(i);

    MAGIC = -1;
    blockCount = 0;

    for (int i = 0; i < M; i++){
        int u = get<1>(edges[i]), v = get<2>(edges[i]);
        unite(u, v);

        if ((i + 1) % blockLength == 0 || i == M - 1){
            int id = blockCount++;

            el[id] = 0, er[id] = i;
            if (id > 0) el[id] = er[id - 1] + 1;

            for (int j = 0; j < N; j++) mem[j][id] = t[leader(j)];
        }

    }

    for (int i = 0; i < N; i++) flag[i] = false;
}

bool can(int x, int y){
    x = leader(x);
    y = leader(y);
    return x == y && !t[x].line;
}

int getMinimumFuelCapacity(int X, int Y) {
    int id = 0;
    while (id < blockCount){
        int xx = mem[X][id].parent, yy = mem[Y][id].parent;
        if (xx == yy && !mem[xx][id].line){
            break;
        } else {
            id++;
        }
    }

    if (id == blockCount) return -1;

    MAGIC = id;

    int ans = -1;

    _nxt = 0;

    for (int i = el[id]; i <= er[id]; i++){
        auto [w, u, v] = edges[i];
        unite(u, v);
        if (can(X, Y)){
            ans = w;
            break;
        }
    }

    for (int i = 0; i < _nxt; i++) flag[re[i]] = false;

    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 188 ms 354048 KB Output is correct
2 Correct 164 ms 354108 KB Output is correct
3 Correct 155 ms 354064 KB Output is correct
4 Correct 163 ms 354040 KB Output is correct
5 Correct 158 ms 354128 KB Output is correct
6 Correct 157 ms 354088 KB Output is correct
7 Correct 194 ms 354108 KB Output is correct
8 Correct 156 ms 354080 KB Output is correct
9 Correct 424 ms 356904 KB Output is correct
10 Correct 560 ms 357520 KB Output is correct
11 Correct 563 ms 357528 KB Output is correct
12 Correct 587 ms 357708 KB Output is correct
13 Correct 616 ms 357732 KB Output is correct
14 Correct 429 ms 357160 KB Output is correct
15 Correct 716 ms 359364 KB Output is correct
16 Correct 740 ms 359416 KB Output is correct
17 Correct 769 ms 359464 KB Output is correct
18 Correct 787 ms 359456 KB Output is correct
19 Correct 640 ms 358144 KB Output is correct
20 Execution timed out 2049 ms 359324 KB Time limit exceeded
21 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 188 ms 354048 KB Output is correct
2 Correct 164 ms 354108 KB Output is correct
3 Execution timed out 2058 ms 358968 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 188 ms 354048 KB Output is correct
2 Correct 164 ms 354108 KB Output is correct
3 Correct 155 ms 354064 KB Output is correct
4 Correct 163 ms 354040 KB Output is correct
5 Correct 158 ms 354128 KB Output is correct
6 Correct 157 ms 354088 KB Output is correct
7 Correct 194 ms 354108 KB Output is correct
8 Correct 156 ms 354080 KB Output is correct
9 Correct 165 ms 354096 KB Output is correct
10 Correct 174 ms 354136 KB Output is correct
11 Correct 159 ms 354132 KB Output is correct
12 Correct 156 ms 354192 KB Output is correct
13 Correct 156 ms 354140 KB Output is correct
14 Correct 159 ms 354124 KB Output is correct
15 Correct 158 ms 354124 KB Output is correct
16 Correct 153 ms 354152 KB Output is correct
17 Correct 165 ms 354380 KB Output is correct
18 Correct 159 ms 354072 KB Output is correct
19 Correct 157 ms 354100 KB Output is correct
20 Correct 159 ms 354076 KB Output is correct
21 Correct 165 ms 354064 KB Output is correct
22 Correct 159 ms 354144 KB Output is correct
23 Correct 166 ms 354128 KB Output is correct
24 Correct 157 ms 354124 KB Output is correct
25 Correct 169 ms 354112 KB Output is correct
26 Correct 169 ms 354220 KB Output is correct
27 Correct 157 ms 354148 KB Output is correct
28 Correct 164 ms 354196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 165 ms 354096 KB Output is correct
2 Correct 188 ms 354048 KB Output is correct
3 Correct 164 ms 354108 KB Output is correct
4 Correct 155 ms 354064 KB Output is correct
5 Correct 163 ms 354040 KB Output is correct
6 Correct 158 ms 354128 KB Output is correct
7 Correct 157 ms 354088 KB Output is correct
8 Correct 194 ms 354108 KB Output is correct
9 Correct 156 ms 354080 KB Output is correct
10 Correct 424 ms 356904 KB Output is correct
11 Correct 560 ms 357520 KB Output is correct
12 Correct 563 ms 357528 KB Output is correct
13 Correct 587 ms 357708 KB Output is correct
14 Correct 616 ms 357732 KB Output is correct
15 Correct 174 ms 354136 KB Output is correct
16 Correct 159 ms 354132 KB Output is correct
17 Correct 156 ms 354192 KB Output is correct
18 Correct 156 ms 354140 KB Output is correct
19 Correct 159 ms 354124 KB Output is correct
20 Correct 158 ms 354124 KB Output is correct
21 Correct 153 ms 354152 KB Output is correct
22 Correct 165 ms 354380 KB Output is correct
23 Correct 159 ms 354072 KB Output is correct
24 Correct 157 ms 354100 KB Output is correct
25 Correct 159 ms 354076 KB Output is correct
26 Correct 165 ms 354064 KB Output is correct
27 Correct 159 ms 354144 KB Output is correct
28 Correct 166 ms 354128 KB Output is correct
29 Correct 157 ms 354124 KB Output is correct
30 Correct 169 ms 354112 KB Output is correct
31 Correct 169 ms 354220 KB Output is correct
32 Correct 157 ms 354148 KB Output is correct
33 Correct 164 ms 354196 KB Output is correct
34 Correct 167 ms 354596 KB Output is correct
35 Correct 586 ms 357660 KB Output is correct
36 Correct 559 ms 357656 KB Output is correct
37 Correct 562 ms 357708 KB Output is correct
38 Correct 569 ms 357632 KB Output is correct
39 Correct 599 ms 357592 KB Output is correct
40 Correct 508 ms 357308 KB Output is correct
41 Correct 583 ms 357664 KB Output is correct
42 Correct 597 ms 357696 KB Output is correct
43 Correct 578 ms 357664 KB Output is correct
44 Correct 576 ms 357708 KB Output is correct
45 Runtime error 974 ms 524288 KB Execution killed with signal 11
46 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 188 ms 354048 KB Output is correct
2 Correct 164 ms 354108 KB Output is correct
3 Correct 155 ms 354064 KB Output is correct
4 Correct 163 ms 354040 KB Output is correct
5 Correct 158 ms 354128 KB Output is correct
6 Correct 157 ms 354088 KB Output is correct
7 Correct 194 ms 354108 KB Output is correct
8 Correct 156 ms 354080 KB Output is correct
9 Correct 424 ms 356904 KB Output is correct
10 Correct 560 ms 357520 KB Output is correct
11 Correct 563 ms 357528 KB Output is correct
12 Correct 587 ms 357708 KB Output is correct
13 Correct 616 ms 357732 KB Output is correct
14 Correct 429 ms 357160 KB Output is correct
15 Correct 716 ms 359364 KB Output is correct
16 Correct 740 ms 359416 KB Output is correct
17 Correct 769 ms 359464 KB Output is correct
18 Correct 787 ms 359456 KB Output is correct
19 Execution timed out 2058 ms 358968 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 165 ms 354096 KB Output is correct
2 Correct 188 ms 354048 KB Output is correct
3 Correct 164 ms 354108 KB Output is correct
4 Correct 155 ms 354064 KB Output is correct
5 Correct 163 ms 354040 KB Output is correct
6 Correct 158 ms 354128 KB Output is correct
7 Correct 157 ms 354088 KB Output is correct
8 Correct 194 ms 354108 KB Output is correct
9 Correct 156 ms 354080 KB Output is correct
10 Correct 424 ms 356904 KB Output is correct
11 Correct 560 ms 357520 KB Output is correct
12 Correct 563 ms 357528 KB Output is correct
13 Correct 587 ms 357708 KB Output is correct
14 Correct 616 ms 357732 KB Output is correct
15 Correct 429 ms 357160 KB Output is correct
16 Correct 716 ms 359364 KB Output is correct
17 Correct 740 ms 359416 KB Output is correct
18 Correct 769 ms 359464 KB Output is correct
19 Correct 787 ms 359456 KB Output is correct
20 Execution timed out 2058 ms 358968 KB Time limit exceeded
21 Halted 0 ms 0 KB -