Submission #1047799

# Submission time Handle Problem Language Result Execution time Memory
1047799 2024-08-07T16:02:51 Z stdfloat Swapping Cities (APIO20_swap) C++17
100 / 100
293 ms 70520 KB
#include <bits/stdc++.h>
#include "swap.h"
using namespace std;
 
using ll = long long;
 
#define ff  first
#define ss  second
#define pii pair<int, int>
 
int aux;

vector<bool> swp;

vector<int> p, sz, cnt, w, h;

vector<vector<int>> sp;

vector<vector<int>> E;

int fnd(int x) {
    return p[x] = (p[x] == x ? x : fnd(p[x]));
}

void dfs(int x) {
    for (auto i : E[x]) {
        h[i] = h[x] + 1;
        dfs(i);
    }
}

void init(int n, int m, vector<int> U, vector<int> V, vector<int> Wi) {
    aux = n;
    cnt.assign(n, 0);
    w.assign(n + m, 0);
    p.assign(n + m, 0);
    sz.assign(n + m, 1);
    E.assign(n + m, {});
    swp.assign(n + m, false);
    sp.assign(n + m, vector<int>(20, -1));

    iota(p.begin(), p.end(), 0);

    vector<pair<int, pii>> Ee;
    for (int i = 0; i < m; i++) {
        Ee.push_back({Wi[i], {U[i], V[i]}});
    }

    sort(Ee.begin(), Ee.end());
    for (auto i : Ee) {
        int x = fnd(i.ss.ff), y = fnd(i.ss.ss);

        cnt[i.ss.ff]++; cnt[i.ss.ss]++;

        E[aux].push_back(x);
        if (x != y) E[aux].push_back(y);

        p[x] = p[y] = sp[x][0] = sp[y][0] = aux;

        w[aux] = i.ff;
        swp[aux] = (x == y || swp[x] || swp[y] || max(cnt[i.ss.ff], cnt[i.ss.ss]) > 2);

        aux++;
    }

    for (int i = 1; i < 20; i++) {
        for (int j = 0; j < n + m; j++) {
            if (sp[j][i - 1] != -1) sp[j][i] = sp[sp[j][i - 1]][i - 1];
        }
    }

    h.assign(aux, 0);
    dfs(aux - 1);
}
 
int getMinimumFuelCapacity(int x, int y) {
    if (h[x] > h[y]) swap(x, y);

    for (int i = 19; i >= 0; i--) {
        if (h[x] <= h[y] - (1 << i)) y = sp[y][i];
    }

    for (int i = 19; i >= 0; i--) {
        if (sp[x][i] != sp[y][i]) {
            x = sp[x][i]; y = sp[y][i];
        }
    }

    x = sp[x][0];

    if (swp[x]) return w[x];

    for (int i = 19; i >= 0; i--) {
        if (sp[x][i] != -1 && !swp[sp[x][i]]) x = sp[x][i];
    }

    return (sp[x][0] == -1 ? -1 : w[sp[x][0]]);
}
# Verdict Execution time Memory 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 1 ms 348 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 704 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 56 ms 31432 KB Output is correct
10 Correct 71 ms 38408 KB Output is correct
11 Correct 69 ms 37832 KB Output is correct
12 Correct 70 ms 39880 KB Output is correct
13 Correct 66 ms 42444 KB Output is correct
14 Correct 66 ms 31832 KB Output is correct
15 Correct 158 ms 41416 KB Output is correct
16 Correct 154 ms 40136 KB Output is correct
17 Correct 176 ms 42712 KB Output is correct
18 Correct 264 ms 45164 KB Output is correct
19 Correct 56 ms 10996 KB Output is correct
20 Correct 170 ms 41808 KB Output is correct
21 Correct 163 ms 40544 KB Output is correct
22 Correct 171 ms 43372 KB Output is correct
23 Correct 293 ms 45620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 170 ms 45076 KB Output is correct
4 Correct 204 ms 47412 KB Output is correct
5 Correct 242 ms 46052 KB Output is correct
6 Correct 211 ms 46752 KB Output is correct
7 Correct 183 ms 46756 KB Output is correct
8 Correct 204 ms 44584 KB Output is correct
9 Correct 218 ms 46348 KB Output is correct
10 Correct 213 ms 44604 KB Output is correct
# Verdict Execution time Memory 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 1 ms 348 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 704 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 700 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 704 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 740 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
26 Correct 1 ms 1116 KB Output is correct
27 Correct 1 ms 860 KB Output is correct
28 Correct 1 ms 940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 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 1 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 704 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 56 ms 31432 KB Output is correct
11 Correct 71 ms 38408 KB Output is correct
12 Correct 69 ms 37832 KB Output is correct
13 Correct 70 ms 39880 KB Output is correct
14 Correct 66 ms 42444 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 700 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 704 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 740 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 860 KB Output is correct
30 Correct 1 ms 860 KB Output is correct
31 Correct 1 ms 1116 KB Output is correct
32 Correct 1 ms 860 KB Output is correct
33 Correct 1 ms 940 KB Output is correct
34 Correct 14 ms 5916 KB Output is correct
35 Correct 74 ms 39880 KB Output is correct
36 Correct 82 ms 39872 KB Output is correct
37 Correct 74 ms 39876 KB Output is correct
38 Correct 76 ms 39484 KB Output is correct
39 Correct 77 ms 39032 KB Output is correct
40 Correct 83 ms 35784 KB Output is correct
41 Correct 79 ms 39872 KB Output is correct
42 Correct 73 ms 39876 KB Output is correct
43 Correct 75 ms 42952 KB Output is correct
44 Correct 77 ms 40128 KB Output is correct
45 Correct 100 ms 53300 KB Output is correct
46 Correct 68 ms 39880 KB Output is correct
47 Correct 74 ms 40012 KB Output is correct
48 Correct 79 ms 43720 KB Output is correct
49 Correct 72 ms 44512 KB Output is correct
50 Correct 58 ms 35528 KB Output is correct
51 Correct 83 ms 44848 KB Output is correct
52 Correct 112 ms 57280 KB Output is correct
53 Correct 126 ms 60920 KB Output is correct
54 Correct 135 ms 68288 KB Output is correct
55 Correct 69 ms 42696 KB Output is correct
56 Correct 130 ms 62472 KB Output is correct
# Verdict Execution time Memory 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 1 ms 348 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 704 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 56 ms 31432 KB Output is correct
10 Correct 71 ms 38408 KB Output is correct
11 Correct 69 ms 37832 KB Output is correct
12 Correct 70 ms 39880 KB Output is correct
13 Correct 66 ms 42444 KB Output is correct
14 Correct 66 ms 31832 KB Output is correct
15 Correct 158 ms 41416 KB Output is correct
16 Correct 154 ms 40136 KB Output is correct
17 Correct 176 ms 42712 KB Output is correct
18 Correct 264 ms 45164 KB Output is correct
19 Correct 170 ms 45076 KB Output is correct
20 Correct 204 ms 47412 KB Output is correct
21 Correct 242 ms 46052 KB Output is correct
22 Correct 211 ms 46752 KB Output is correct
23 Correct 183 ms 46756 KB Output is correct
24 Correct 204 ms 44584 KB Output is correct
25 Correct 218 ms 46348 KB Output is correct
26 Correct 213 ms 44604 KB Output is correct
27 Correct 1 ms 600 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 700 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 14 ms 5916 KB Output is correct
37 Correct 74 ms 39880 KB Output is correct
38 Correct 82 ms 39872 KB Output is correct
39 Correct 74 ms 39876 KB Output is correct
40 Correct 76 ms 39484 KB Output is correct
41 Correct 77 ms 39032 KB Output is correct
42 Correct 83 ms 35784 KB Output is correct
43 Correct 79 ms 39872 KB Output is correct
44 Correct 73 ms 39876 KB Output is correct
45 Correct 75 ms 42952 KB Output is correct
46 Correct 77 ms 40128 KB Output is correct
47 Correct 15 ms 5768 KB Output is correct
48 Correct 129 ms 42636 KB Output is correct
49 Correct 138 ms 42444 KB Output is correct
50 Correct 126 ms 42340 KB Output is correct
51 Correct 129 ms 42444 KB Output is correct
52 Correct 128 ms 39976 KB Output is correct
53 Correct 107 ms 30456 KB Output is correct
54 Correct 151 ms 43416 KB Output is correct
55 Correct 132 ms 42696 KB Output is correct
56 Correct 195 ms 44996 KB Output is correct
57 Correct 144 ms 43412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 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 1 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 704 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 56 ms 31432 KB Output is correct
11 Correct 71 ms 38408 KB Output is correct
12 Correct 69 ms 37832 KB Output is correct
13 Correct 70 ms 39880 KB Output is correct
14 Correct 66 ms 42444 KB Output is correct
15 Correct 66 ms 31832 KB Output is correct
16 Correct 158 ms 41416 KB Output is correct
17 Correct 154 ms 40136 KB Output is correct
18 Correct 176 ms 42712 KB Output is correct
19 Correct 264 ms 45164 KB Output is correct
20 Correct 170 ms 45076 KB Output is correct
21 Correct 204 ms 47412 KB Output is correct
22 Correct 242 ms 46052 KB Output is correct
23 Correct 211 ms 46752 KB Output is correct
24 Correct 183 ms 46756 KB Output is correct
25 Correct 204 ms 44584 KB Output is correct
26 Correct 218 ms 46348 KB Output is correct
27 Correct 213 ms 44604 KB Output is correct
28 Correct 1 ms 600 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 700 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 1 ms 604 KB Output is correct
37 Correct 14 ms 5916 KB Output is correct
38 Correct 74 ms 39880 KB Output is correct
39 Correct 82 ms 39872 KB Output is correct
40 Correct 74 ms 39876 KB Output is correct
41 Correct 76 ms 39484 KB Output is correct
42 Correct 77 ms 39032 KB Output is correct
43 Correct 83 ms 35784 KB Output is correct
44 Correct 79 ms 39872 KB Output is correct
45 Correct 73 ms 39876 KB Output is correct
46 Correct 75 ms 42952 KB Output is correct
47 Correct 77 ms 40128 KB Output is correct
48 Correct 15 ms 5768 KB Output is correct
49 Correct 129 ms 42636 KB Output is correct
50 Correct 138 ms 42444 KB Output is correct
51 Correct 126 ms 42340 KB Output is correct
52 Correct 129 ms 42444 KB Output is correct
53 Correct 128 ms 39976 KB Output is correct
54 Correct 107 ms 30456 KB Output is correct
55 Correct 151 ms 43416 KB Output is correct
56 Correct 132 ms 42696 KB Output is correct
57 Correct 195 ms 44996 KB Output is correct
58 Correct 144 ms 43412 KB Output is correct
59 Correct 56 ms 10996 KB Output is correct
60 Correct 170 ms 41808 KB Output is correct
61 Correct 163 ms 40544 KB Output is correct
62 Correct 171 ms 43372 KB Output is correct
63 Correct 293 ms 45620 KB Output is correct
64 Correct 1 ms 604 KB Output is correct
65 Correct 1 ms 704 KB Output is correct
66 Correct 1 ms 604 KB Output is correct
67 Correct 1 ms 740 KB Output is correct
68 Correct 1 ms 604 KB Output is correct
69 Correct 1 ms 860 KB Output is correct
70 Correct 1 ms 860 KB Output is correct
71 Correct 1 ms 1116 KB Output is correct
72 Correct 1 ms 860 KB Output is correct
73 Correct 1 ms 940 KB Output is correct
74 Correct 100 ms 53300 KB Output is correct
75 Correct 68 ms 39880 KB Output is correct
76 Correct 74 ms 40012 KB Output is correct
77 Correct 79 ms 43720 KB Output is correct
78 Correct 72 ms 44512 KB Output is correct
79 Correct 58 ms 35528 KB Output is correct
80 Correct 83 ms 44848 KB Output is correct
81 Correct 112 ms 57280 KB Output is correct
82 Correct 126 ms 60920 KB Output is correct
83 Correct 135 ms 68288 KB Output is correct
84 Correct 69 ms 42696 KB Output is correct
85 Correct 130 ms 62472 KB Output is correct
86 Correct 50 ms 19144 KB Output is correct
87 Correct 125 ms 42696 KB Output is correct
88 Correct 148 ms 42600 KB Output is correct
89 Correct 170 ms 44488 KB Output is correct
90 Correct 126 ms 49852 KB Output is correct
91 Correct 141 ms 46012 KB Output is correct
92 Correct 177 ms 45508 KB Output is correct
93 Correct 179 ms 59840 KB Output is correct
94 Correct 210 ms 63576 KB Output is correct
95 Correct 197 ms 70520 KB Output is correct
96 Correct 191 ms 45256 KB Output is correct
97 Correct 197 ms 55200 KB Output is correct