Submission #714974

# Submission time Handle Problem Language Result Execution time Memory
714974 2023-03-25T15:42:21 Z dxz05 Swapping Cities (APIO20_swap) C++17
37 / 100
2000 ms 494268 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 = 200002;

int N, M;
const int blockLength = 650;

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 223 ms 487156 KB Output is correct
2 Correct 224 ms 487088 KB Output is correct
3 Correct 214 ms 487192 KB Output is correct
4 Correct 222 ms 487148 KB Output is correct
5 Correct 219 ms 487244 KB Output is correct
6 Correct 217 ms 487192 KB Output is correct
7 Correct 223 ms 487144 KB Output is correct
8 Correct 233 ms 487208 KB Output is correct
9 Correct 463 ms 489988 KB Output is correct
10 Correct 594 ms 490612 KB Output is correct
11 Correct 503 ms 490552 KB Output is correct
12 Correct 523 ms 490740 KB Output is correct
13 Correct 542 ms 490748 KB Output is correct
14 Correct 438 ms 490128 KB Output is correct
15 Correct 721 ms 492472 KB Output is correct
16 Correct 658 ms 492372 KB Output is correct
17 Correct 668 ms 492528 KB Output is correct
18 Correct 698 ms 492544 KB Output is correct
19 Execution timed out 2097 ms 489536 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 223 ms 487156 KB Output is correct
2 Correct 224 ms 487088 KB Output is correct
3 Execution timed out 2081 ms 492044 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 223 ms 487156 KB Output is correct
2 Correct 224 ms 487088 KB Output is correct
3 Correct 214 ms 487192 KB Output is correct
4 Correct 222 ms 487148 KB Output is correct
5 Correct 219 ms 487244 KB Output is correct
6 Correct 217 ms 487192 KB Output is correct
7 Correct 223 ms 487144 KB Output is correct
8 Correct 233 ms 487208 KB Output is correct
9 Correct 220 ms 487212 KB Output is correct
10 Correct 215 ms 487192 KB Output is correct
11 Correct 221 ms 487292 KB Output is correct
12 Correct 216 ms 487232 KB Output is correct
13 Correct 226 ms 487116 KB Output is correct
14 Correct 214 ms 487204 KB Output is correct
15 Correct 218 ms 487180 KB Output is correct
16 Correct 215 ms 487172 KB Output is correct
17 Correct 213 ms 487192 KB Output is correct
18 Correct 215 ms 487440 KB Output is correct
19 Correct 220 ms 487236 KB Output is correct
20 Correct 215 ms 487132 KB Output is correct
21 Correct 223 ms 487216 KB Output is correct
22 Correct 214 ms 487172 KB Output is correct
23 Correct 220 ms 487276 KB Output is correct
24 Correct 216 ms 487240 KB Output is correct
25 Correct 219 ms 487280 KB Output is correct
26 Correct 218 ms 487160 KB Output is correct
27 Correct 215 ms 487244 KB Output is correct
28 Correct 221 ms 487176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 220 ms 487212 KB Output is correct
2 Correct 223 ms 487156 KB Output is correct
3 Correct 224 ms 487088 KB Output is correct
4 Correct 214 ms 487192 KB Output is correct
5 Correct 222 ms 487148 KB Output is correct
6 Correct 219 ms 487244 KB Output is correct
7 Correct 217 ms 487192 KB Output is correct
8 Correct 223 ms 487144 KB Output is correct
9 Correct 233 ms 487208 KB Output is correct
10 Correct 463 ms 489988 KB Output is correct
11 Correct 594 ms 490612 KB Output is correct
12 Correct 503 ms 490552 KB Output is correct
13 Correct 523 ms 490740 KB Output is correct
14 Correct 542 ms 490748 KB Output is correct
15 Correct 215 ms 487192 KB Output is correct
16 Correct 221 ms 487292 KB Output is correct
17 Correct 216 ms 487232 KB Output is correct
18 Correct 226 ms 487116 KB Output is correct
19 Correct 214 ms 487204 KB Output is correct
20 Correct 218 ms 487180 KB Output is correct
21 Correct 215 ms 487172 KB Output is correct
22 Correct 213 ms 487192 KB Output is correct
23 Correct 215 ms 487440 KB Output is correct
24 Correct 220 ms 487236 KB Output is correct
25 Correct 215 ms 487132 KB Output is correct
26 Correct 223 ms 487216 KB Output is correct
27 Correct 214 ms 487172 KB Output is correct
28 Correct 220 ms 487276 KB Output is correct
29 Correct 216 ms 487240 KB Output is correct
30 Correct 219 ms 487280 KB Output is correct
31 Correct 218 ms 487160 KB Output is correct
32 Correct 215 ms 487244 KB Output is correct
33 Correct 221 ms 487176 KB Output is correct
34 Correct 223 ms 487608 KB Output is correct
35 Correct 528 ms 490740 KB Output is correct
36 Correct 561 ms 490784 KB Output is correct
37 Correct 524 ms 490828 KB Output is correct
38 Correct 501 ms 490700 KB Output is correct
39 Correct 508 ms 490756 KB Output is correct
40 Correct 455 ms 490580 KB Output is correct
41 Correct 507 ms 490824 KB Output is correct
42 Correct 519 ms 490864 KB Output is correct
43 Correct 528 ms 490744 KB Output is correct
44 Correct 518 ms 490824 KB Output is correct
45 Correct 631 ms 492400 KB Output is correct
46 Correct 534 ms 490820 KB Output is correct
47 Correct 581 ms 490744 KB Output is correct
48 Correct 572 ms 491204 KB Output is correct
49 Correct 267 ms 492704 KB Output is correct
50 Correct 273 ms 491424 KB Output is correct
51 Correct 487 ms 491724 KB Output is correct
52 Correct 745 ms 492908 KB Output is correct
53 Correct 805 ms 493640 KB Output is correct
54 Correct 941 ms 494268 KB Output is correct
55 Correct 552 ms 490848 KB Output is correct
56 Correct 819 ms 493288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 223 ms 487156 KB Output is correct
2 Correct 224 ms 487088 KB Output is correct
3 Correct 214 ms 487192 KB Output is correct
4 Correct 222 ms 487148 KB Output is correct
5 Correct 219 ms 487244 KB Output is correct
6 Correct 217 ms 487192 KB Output is correct
7 Correct 223 ms 487144 KB Output is correct
8 Correct 233 ms 487208 KB Output is correct
9 Correct 463 ms 489988 KB Output is correct
10 Correct 594 ms 490612 KB Output is correct
11 Correct 503 ms 490552 KB Output is correct
12 Correct 523 ms 490740 KB Output is correct
13 Correct 542 ms 490748 KB Output is correct
14 Correct 438 ms 490128 KB Output is correct
15 Correct 721 ms 492472 KB Output is correct
16 Correct 658 ms 492372 KB Output is correct
17 Correct 668 ms 492528 KB Output is correct
18 Correct 698 ms 492544 KB Output is correct
19 Execution timed out 2081 ms 492044 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 220 ms 487212 KB Output is correct
2 Correct 223 ms 487156 KB Output is correct
3 Correct 224 ms 487088 KB Output is correct
4 Correct 214 ms 487192 KB Output is correct
5 Correct 222 ms 487148 KB Output is correct
6 Correct 219 ms 487244 KB Output is correct
7 Correct 217 ms 487192 KB Output is correct
8 Correct 223 ms 487144 KB Output is correct
9 Correct 233 ms 487208 KB Output is correct
10 Correct 463 ms 489988 KB Output is correct
11 Correct 594 ms 490612 KB Output is correct
12 Correct 503 ms 490552 KB Output is correct
13 Correct 523 ms 490740 KB Output is correct
14 Correct 542 ms 490748 KB Output is correct
15 Correct 438 ms 490128 KB Output is correct
16 Correct 721 ms 492472 KB Output is correct
17 Correct 658 ms 492372 KB Output is correct
18 Correct 668 ms 492528 KB Output is correct
19 Correct 698 ms 492544 KB Output is correct
20 Execution timed out 2081 ms 492044 KB Time limit exceeded
21 Halted 0 ms 0 KB -