Submission #777872

# Submission time Handle Problem Language Result Execution time Memory
777872 2023-07-09T19:53:26 Z t6twotwo Swapping Cities (APIO20_swap) C++17
100 / 100
378 ms 84172 KB
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
int N, M;
vector<int> ans, dep, p;
vector<vector<int>> par;
constexpr int inf = 2e9;
void init(int n, int m, vector<int> U, vector<int> V, vector<int> W) {
    N = n;
    M = m;
    p.resize(N + M);
    iota(p.begin(), p.end(), 0);
    function<int(int)> find = [&](int x) {
        return x == p[x] ? x : p[x] = find(p[x]);
    };
    vector<int> dif(N + M, -1), three(N + M), deg(N), w(N + M);
    vector<vector<int>> ch(N + M);
    auto unite = [&](int x, int y, int t) {
        x = find(x);
        y = find(y);
        dif[t] = 1;
        for (int z : set<int>{x, y}) {
            three[t] |= three[z];
            dif[t] += dif[z];
            p[z] = t;
            ch[t].push_back(z);
        }
    };
    vector<tuple<int, int, int>> edges(M);
    for (int i = 0; i < M; i++) {
        edges[i] = {W[i], U[i], V[i]};
    }
    sort(edges.begin(), edges.end());
    vector<bool> f(N + M);
    for (int i = 0; i < M; i++) {
        auto [z, x, y] = edges[i];
        if (++deg[x] == 3) {
            three[find(x)] = 1;
        }
        if (++deg[y] == 3) {
            three[find(y)] = 1;
        }
        w[N + i] = z;
        unite(x, y, N + i);
        f[N + i] = three[N + i] == 1 || dif[N + i] >= 0;
    }
    ans.resize(N + M);
    dep.resize(N + M);
    par.resize(N + M, vector<int>(20, -1));
    vector<int> stk{inf};
    auto dfs = [&](auto dfs, int x) -> void {
        for (int i = 0; i < 19 && par[x][i] != -1; i++) {
            par[x][i + 1] = par[par[x][i]][i];
        }
        if (f[x]) {
            stk.push_back(w[x]);
        }
        ans[x] = stk.back();
        for (int y : ch[x]) {
            dep[y] = dep[x] + 1;
            par[y][0] = x;
            dfs(dfs, y);
        }
        if (f[x]) {
            stk.pop_back();
        }
    };
    for (int i = 0; i < N + M; i++) {
        if (find(i) == i) {
            dfs(dfs, i);
        }
    }
}
int getMinimumFuelCapacity(int X, int Y) {
    if (p[X] != p[Y]) {
        return -1;
    }
    if (dep[X] < dep[Y]) {
        swap(X, Y);
    }
    for (int i = 19; i >= 0; i--) {
        if (dep[X] - (1 << i) >= dep[Y]) {
            X = par[X][i];
        }
    }
    for (int i = 19; i >= 0; i--) {
        if (par[X][i] != par[Y][i]) {
            X = par[X][i];
            Y = par[Y][i];
        }
    }
    int Z = par[X][0];
    return ans[Z] == inf ? -1 : ans[Z];
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 106 ms 31552 KB Output is correct
10 Correct 100 ms 38668 KB Output is correct
11 Correct 102 ms 38036 KB Output is correct
12 Correct 107 ms 40248 KB Output is correct
13 Correct 134 ms 46520 KB Output is correct
14 Correct 102 ms 31664 KB Output is correct
15 Correct 217 ms 40884 KB Output is correct
16 Correct 240 ms 39596 KB Output is correct
17 Correct 211 ms 42072 KB Output is correct
18 Correct 378 ms 48280 KB Output is correct
19 Correct 65 ms 9388 KB Output is correct
20 Correct 203 ms 44832 KB Output is correct
21 Correct 204 ms 43332 KB Output is correct
22 Correct 215 ms 46132 KB Output is correct
23 Correct 355 ms 52628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 280 ms 51872 KB Output is correct
4 Correct 298 ms 54800 KB Output is correct
5 Correct 285 ms 52848 KB Output is correct
6 Correct 294 ms 54468 KB Output is correct
7 Correct 322 ms 53736 KB Output is correct
8 Correct 267 ms 51584 KB Output is correct
9 Correct 327 ms 53404 KB Output is correct
10 Correct 267 ms 51060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 696 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 1 ms 692 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
17 Correct 2 ms 724 KB Output is correct
18 Correct 2 ms 696 KB Output is correct
19 Correct 1 ms 596 KB Output is correct
20 Correct 1 ms 596 KB Output is correct
21 Correct 1 ms 724 KB Output is correct
22 Correct 2 ms 980 KB Output is correct
23 Correct 1 ms 596 KB Output is correct
24 Correct 2 ms 980 KB Output is correct
25 Correct 2 ms 988 KB Output is correct
26 Correct 2 ms 992 KB Output is correct
27 Correct 1 ms 696 KB Output is correct
28 Correct 3 ms 1108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
10 Correct 106 ms 31552 KB Output is correct
11 Correct 100 ms 38668 KB Output is correct
12 Correct 102 ms 38036 KB Output is correct
13 Correct 107 ms 40248 KB Output is correct
14 Correct 134 ms 46520 KB Output is correct
15 Correct 1 ms 696 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
17 Correct 1 ms 596 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 1 ms 596 KB Output is correct
20 Correct 1 ms 692 KB Output is correct
21 Correct 1 ms 596 KB Output is correct
22 Correct 2 ms 724 KB Output is correct
23 Correct 2 ms 696 KB Output is correct
24 Correct 1 ms 596 KB Output is correct
25 Correct 1 ms 596 KB Output is correct
26 Correct 1 ms 724 KB Output is correct
27 Correct 2 ms 980 KB Output is correct
28 Correct 1 ms 596 KB Output is correct
29 Correct 2 ms 980 KB Output is correct
30 Correct 2 ms 988 KB Output is correct
31 Correct 2 ms 992 KB Output is correct
32 Correct 1 ms 696 KB Output is correct
33 Correct 3 ms 1108 KB Output is correct
34 Correct 11 ms 5844 KB Output is correct
35 Correct 109 ms 40392 KB Output is correct
36 Correct 115 ms 40340 KB Output is correct
37 Correct 108 ms 40260 KB Output is correct
38 Correct 109 ms 39824 KB Output is correct
39 Correct 111 ms 39528 KB Output is correct
40 Correct 114 ms 36288 KB Output is correct
41 Correct 108 ms 40396 KB Output is correct
42 Correct 105 ms 40384 KB Output is correct
43 Correct 122 ms 49064 KB Output is correct
44 Correct 128 ms 40424 KB Output is correct
45 Correct 162 ms 59752 KB Output is correct
46 Correct 121 ms 40388 KB Output is correct
47 Correct 136 ms 40640 KB Output is correct
48 Correct 148 ms 47592 KB Output is correct
49 Correct 144 ms 57448 KB Output is correct
50 Correct 95 ms 44596 KB Output is correct
51 Correct 147 ms 52024 KB Output is correct
52 Correct 203 ms 62632 KB Output is correct
53 Correct 221 ms 66060 KB Output is correct
54 Correct 226 ms 76508 KB Output is correct
55 Correct 120 ms 48184 KB Output is correct
56 Correct 206 ms 70252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 106 ms 31552 KB Output is correct
10 Correct 100 ms 38668 KB Output is correct
11 Correct 102 ms 38036 KB Output is correct
12 Correct 107 ms 40248 KB Output is correct
13 Correct 134 ms 46520 KB Output is correct
14 Correct 102 ms 31664 KB Output is correct
15 Correct 217 ms 40884 KB Output is correct
16 Correct 240 ms 39596 KB Output is correct
17 Correct 211 ms 42072 KB Output is correct
18 Correct 378 ms 48280 KB Output is correct
19 Correct 280 ms 51872 KB Output is correct
20 Correct 298 ms 54800 KB Output is correct
21 Correct 285 ms 52848 KB Output is correct
22 Correct 294 ms 54468 KB Output is correct
23 Correct 322 ms 53736 KB Output is correct
24 Correct 267 ms 51584 KB Output is correct
25 Correct 327 ms 53404 KB Output is correct
26 Correct 267 ms 51060 KB Output is correct
27 Correct 1 ms 696 KB Output is correct
28 Correct 1 ms 596 KB Output is correct
29 Correct 1 ms 596 KB Output is correct
30 Correct 1 ms 596 KB Output is correct
31 Correct 1 ms 596 KB Output is correct
32 Correct 1 ms 692 KB Output is correct
33 Correct 1 ms 596 KB Output is correct
34 Correct 2 ms 724 KB Output is correct
35 Correct 2 ms 696 KB Output is correct
36 Correct 11 ms 5844 KB Output is correct
37 Correct 109 ms 40392 KB Output is correct
38 Correct 115 ms 40340 KB Output is correct
39 Correct 108 ms 40260 KB Output is correct
40 Correct 109 ms 39824 KB Output is correct
41 Correct 111 ms 39528 KB Output is correct
42 Correct 114 ms 36288 KB Output is correct
43 Correct 108 ms 40396 KB Output is correct
44 Correct 105 ms 40384 KB Output is correct
45 Correct 122 ms 49064 KB Output is correct
46 Correct 128 ms 40424 KB Output is correct
47 Correct 19 ms 5844 KB Output is correct
48 Correct 229 ms 45968 KB Output is correct
49 Correct 202 ms 45888 KB Output is correct
50 Correct 206 ms 45772 KB Output is correct
51 Correct 203 ms 45628 KB Output is correct
52 Correct 193 ms 43084 KB Output is correct
53 Correct 159 ms 32076 KB Output is correct
54 Correct 242 ms 46184 KB Output is correct
55 Correct 210 ms 45912 KB Output is correct
56 Correct 354 ms 52784 KB Output is correct
57 Correct 227 ms 46412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
10 Correct 106 ms 31552 KB Output is correct
11 Correct 100 ms 38668 KB Output is correct
12 Correct 102 ms 38036 KB Output is correct
13 Correct 107 ms 40248 KB Output is correct
14 Correct 134 ms 46520 KB Output is correct
15 Correct 102 ms 31664 KB Output is correct
16 Correct 217 ms 40884 KB Output is correct
17 Correct 240 ms 39596 KB Output is correct
18 Correct 211 ms 42072 KB Output is correct
19 Correct 378 ms 48280 KB Output is correct
20 Correct 280 ms 51872 KB Output is correct
21 Correct 298 ms 54800 KB Output is correct
22 Correct 285 ms 52848 KB Output is correct
23 Correct 294 ms 54468 KB Output is correct
24 Correct 322 ms 53736 KB Output is correct
25 Correct 267 ms 51584 KB Output is correct
26 Correct 327 ms 53404 KB Output is correct
27 Correct 267 ms 51060 KB Output is correct
28 Correct 1 ms 696 KB Output is correct
29 Correct 1 ms 596 KB Output is correct
30 Correct 1 ms 596 KB Output is correct
31 Correct 1 ms 596 KB Output is correct
32 Correct 1 ms 596 KB Output is correct
33 Correct 1 ms 692 KB Output is correct
34 Correct 1 ms 596 KB Output is correct
35 Correct 2 ms 724 KB Output is correct
36 Correct 2 ms 696 KB Output is correct
37 Correct 11 ms 5844 KB Output is correct
38 Correct 109 ms 40392 KB Output is correct
39 Correct 115 ms 40340 KB Output is correct
40 Correct 108 ms 40260 KB Output is correct
41 Correct 109 ms 39824 KB Output is correct
42 Correct 111 ms 39528 KB Output is correct
43 Correct 114 ms 36288 KB Output is correct
44 Correct 108 ms 40396 KB Output is correct
45 Correct 105 ms 40384 KB Output is correct
46 Correct 122 ms 49064 KB Output is correct
47 Correct 128 ms 40424 KB Output is correct
48 Correct 19 ms 5844 KB Output is correct
49 Correct 229 ms 45968 KB Output is correct
50 Correct 202 ms 45888 KB Output is correct
51 Correct 206 ms 45772 KB Output is correct
52 Correct 203 ms 45628 KB Output is correct
53 Correct 193 ms 43084 KB Output is correct
54 Correct 159 ms 32076 KB Output is correct
55 Correct 242 ms 46184 KB Output is correct
56 Correct 210 ms 45912 KB Output is correct
57 Correct 354 ms 52784 KB Output is correct
58 Correct 227 ms 46412 KB Output is correct
59 Correct 65 ms 9388 KB Output is correct
60 Correct 203 ms 44832 KB Output is correct
61 Correct 204 ms 43332 KB Output is correct
62 Correct 215 ms 46132 KB Output is correct
63 Correct 355 ms 52628 KB Output is correct
64 Correct 1 ms 596 KB Output is correct
65 Correct 1 ms 596 KB Output is correct
66 Correct 1 ms 724 KB Output is correct
67 Correct 2 ms 980 KB Output is correct
68 Correct 1 ms 596 KB Output is correct
69 Correct 2 ms 980 KB Output is correct
70 Correct 2 ms 988 KB Output is correct
71 Correct 2 ms 992 KB Output is correct
72 Correct 1 ms 696 KB Output is correct
73 Correct 3 ms 1108 KB Output is correct
74 Correct 162 ms 59752 KB Output is correct
75 Correct 121 ms 40388 KB Output is correct
76 Correct 136 ms 40640 KB Output is correct
77 Correct 148 ms 47592 KB Output is correct
78 Correct 144 ms 57448 KB Output is correct
79 Correct 95 ms 44596 KB Output is correct
80 Correct 147 ms 52024 KB Output is correct
81 Correct 203 ms 62632 KB Output is correct
82 Correct 221 ms 66060 KB Output is correct
83 Correct 226 ms 76508 KB Output is correct
84 Correct 120 ms 48184 KB Output is correct
85 Correct 206 ms 70252 KB Output is correct
86 Correct 73 ms 22796 KB Output is correct
87 Correct 212 ms 45932 KB Output is correct
88 Correct 198 ms 45900 KB Output is correct
89 Correct 289 ms 51208 KB Output is correct
90 Correct 203 ms 67004 KB Output is correct
91 Correct 206 ms 61292 KB Output is correct
92 Correct 327 ms 55840 KB Output is correct
93 Correct 292 ms 68580 KB Output is correct
94 Correct 371 ms 73648 KB Output is correct
95 Correct 344 ms 84172 KB Output is correct
96 Correct 329 ms 53180 KB Output is correct
97 Correct 327 ms 63320 KB Output is correct