Submission #349527

# Submission time Handle Problem Language Result Execution time Memory
349527 2021-01-17T18:31:04 Z parsabahrami Swapping Cities (APIO20_swap) C++17
53 / 100
359 ms 30636 KB
#include "swap.h"
#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<int, int > pii;

#define SZ(x)               (int) x.size()
#define F                   first
#define S                   second

const int N = 2e5 + 10;
int P[20][N], H[N], C[N], rt[N], D[N], M[N], tim;

int Find(int v) {
    return !rt[v] ? v : rt[v] = Find(rt[v]);
}

void Union(int u, int v, int w) {
    u = Find(u), v = Find(v);
    if (u == v) {
        ++tim;
        P[0][u] = rt[u] = P[0][tim] = tim;
        C[u] = w; M[tim] = 1;
    } else {
        ++tim;
        rt[u] = rt[v] = P[0][u] = P[0][v] = P[0][tim] = tim;
        C[u] = C[v] = w;
        M[tim] = M[u] | M[v];
    }
}

void init(int n, int m,
          std::vector<int> U, std::vector<int> V, std::vector<int> W) {
    tim = n - 1; vector<pair<int, pii>> E;
    for (int i = 0; i < m; i++) {
        E.push_back({W[i], {U[i], V[i]}});
    }
    sort(E.begin(), E.end());
    for (auto e : E) {
        int w = e.F, u = e.S.F, v = e.S.S;
        D[u]++, D[v]++;
        Union(u, v, w);
        if (D[u] > 2 || D[v] > 2) M[Find(u)] = 1;
    }
    for (int i = 1; i < 20; i++) for (int j = 0; j <= tim; j++) P[i][j] = P[i - 1][P[i - 1][j]];
    for (int i = tim - 1; i >= 0; i--) H[i] = H[P[0][i]] + 1;
}

int getMinimumFuelCapacity(int u, int v) {
    if (H[u] < H[v]) swap(u, v);
    for (int i = H[u] - H[v]; i; i -= i & -i) u = P[__builtin_ctz(i)][u];
    for (int i = 19; ~i; i--) {
        if (P[i][u] != P[i][v] || !M[P[i][v]]) {
            u = P[i][u], v = P[i][v];
        }
    }
    if (P[0][u] != P[0][v]) return -1;
    if (!M[P[0][u]]) return -1;
    return C[u];
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 620 KB Output is correct
5 Correct 1 ms 748 KB Output is correct
6 Correct 1 ms 620 KB Output is correct
7 Correct 1 ms 748 KB Output is correct
8 Correct 1 ms 748 KB Output is correct
9 Correct 52 ms 19700 KB Output is correct
10 Correct 64 ms 24032 KB Output is correct
11 Correct 64 ms 23648 KB Output is correct
12 Correct 66 ms 24928 KB Output is correct
13 Correct 67 ms 26464 KB Output is correct
14 Correct 61 ms 20064 KB Output is correct
15 Correct 156 ms 28000 KB Output is correct
16 Correct 154 ms 27352 KB Output is correct
17 Correct 160 ms 28768 KB Output is correct
18 Correct 275 ms 30424 KB Output is correct
19 Correct 89 ms 9324 KB Output is correct
20 Correct 234 ms 28128 KB Output is correct
21 Correct 247 ms 27716 KB Output is correct
22 Correct 250 ms 29100 KB Output is correct
23 Correct 345 ms 30636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 344 ms 27300 KB Output is correct
4 Correct 358 ms 28204 KB Output is correct
5 Correct 359 ms 28040 KB Output is correct
6 Correct 334 ms 28080 KB Output is correct
7 Correct 351 ms 28124 KB Output is correct
8 Correct 350 ms 27196 KB Output is correct
9 Correct 340 ms 28000 KB Output is correct
10 Correct 350 ms 26964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 620 KB Output is correct
6 Correct 1 ms 748 KB Output is correct
7 Correct 1 ms 620 KB Output is correct
8 Correct 1 ms 748 KB Output is correct
9 Correct 1 ms 748 KB Output is correct
10 Correct 1 ms 748 KB Output is correct
11 Correct 1 ms 748 KB Output is correct
12 Correct 1 ms 748 KB Output is correct
13 Correct 1 ms 620 KB Output is correct
14 Correct 1 ms 620 KB Output is correct
15 Correct 1 ms 748 KB Output is correct
16 Correct 1 ms 748 KB Output is correct
17 Correct 1 ms 748 KB Output is correct
18 Correct 1 ms 748 KB Output is correct
19 Correct 1 ms 640 KB Output is correct
20 Correct 1 ms 748 KB Output is correct
21 Correct 1 ms 748 KB Output is correct
22 Correct 2 ms 748 KB Output is correct
23 Correct 1 ms 620 KB Output is correct
24 Correct 2 ms 876 KB Output is correct
25 Correct 2 ms 876 KB Output is correct
26 Correct 2 ms 876 KB Output is correct
27 Correct 1 ms 748 KB Output is correct
28 Correct 2 ms 876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 620 KB Output is correct
6 Correct 1 ms 748 KB Output is correct
7 Correct 1 ms 620 KB Output is correct
8 Correct 1 ms 748 KB Output is correct
9 Correct 1 ms 748 KB Output is correct
10 Correct 52 ms 19700 KB Output is correct
11 Correct 64 ms 24032 KB Output is correct
12 Correct 64 ms 23648 KB Output is correct
13 Correct 66 ms 24928 KB Output is correct
14 Correct 67 ms 26464 KB Output is correct
15 Correct 1 ms 748 KB Output is correct
16 Correct 1 ms 748 KB Output is correct
17 Correct 1 ms 748 KB Output is correct
18 Correct 1 ms 620 KB Output is correct
19 Correct 1 ms 620 KB Output is correct
20 Correct 1 ms 748 KB Output is correct
21 Correct 1 ms 748 KB Output is correct
22 Correct 1 ms 748 KB Output is correct
23 Correct 1 ms 748 KB Output is correct
24 Correct 9 ms 3884 KB Output is correct
25 Correct 63 ms 24544 KB Output is correct
26 Correct 65 ms 24672 KB Output is correct
27 Correct 63 ms 24544 KB Output is correct
28 Correct 63 ms 24288 KB Output is correct
29 Correct 63 ms 24288 KB Output is correct
30 Correct 59 ms 22260 KB Output is correct
31 Correct 68 ms 25056 KB Output is correct
32 Correct 64 ms 24544 KB Output is correct
33 Correct 61 ms 24544 KB Output is correct
34 Correct 68 ms 24928 KB Output is correct
35 Correct 1 ms 640 KB Output is correct
36 Correct 1 ms 748 KB Output is correct
37 Correct 1 ms 748 KB Output is correct
38 Correct 2 ms 748 KB Output is correct
39 Correct 1 ms 620 KB Output is correct
40 Correct 2 ms 876 KB Output is correct
41 Correct 2 ms 876 KB Output is correct
42 Correct 2 ms 876 KB Output is correct
43 Correct 1 ms 748 KB Output is correct
44 Correct 2 ms 876 KB Output is correct
45 Runtime error 103 ms 20700 KB Execution killed with signal 11 (could be triggered by violating memory limits)
46 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 620 KB Output is correct
5 Correct 1 ms 748 KB Output is correct
6 Correct 1 ms 620 KB Output is correct
7 Correct 1 ms 748 KB Output is correct
8 Correct 1 ms 748 KB Output is correct
9 Correct 52 ms 19700 KB Output is correct
10 Correct 64 ms 24032 KB Output is correct
11 Correct 64 ms 23648 KB Output is correct
12 Correct 66 ms 24928 KB Output is correct
13 Correct 67 ms 26464 KB Output is correct
14 Correct 61 ms 20064 KB Output is correct
15 Correct 156 ms 28000 KB Output is correct
16 Correct 154 ms 27352 KB Output is correct
17 Correct 160 ms 28768 KB Output is correct
18 Correct 275 ms 30424 KB Output is correct
19 Correct 344 ms 27300 KB Output is correct
20 Correct 358 ms 28204 KB Output is correct
21 Correct 359 ms 28040 KB Output is correct
22 Correct 334 ms 28080 KB Output is correct
23 Correct 351 ms 28124 KB Output is correct
24 Correct 350 ms 27196 KB Output is correct
25 Correct 340 ms 28000 KB Output is correct
26 Correct 350 ms 26964 KB Output is correct
27 Correct 1 ms 748 KB Output is correct
28 Correct 1 ms 748 KB Output is correct
29 Correct 1 ms 748 KB Output is correct
30 Correct 1 ms 620 KB Output is correct
31 Correct 1 ms 620 KB Output is correct
32 Correct 1 ms 748 KB Output is correct
33 Correct 1 ms 748 KB Output is correct
34 Correct 1 ms 748 KB Output is correct
35 Correct 1 ms 748 KB Output is correct
36 Correct 9 ms 3884 KB Output is correct
37 Correct 63 ms 24544 KB Output is correct
38 Correct 65 ms 24672 KB Output is correct
39 Correct 63 ms 24544 KB Output is correct
40 Correct 63 ms 24288 KB Output is correct
41 Correct 63 ms 24288 KB Output is correct
42 Correct 59 ms 22260 KB Output is correct
43 Correct 68 ms 25056 KB Output is correct
44 Correct 64 ms 24544 KB Output is correct
45 Correct 61 ms 24544 KB Output is correct
46 Correct 68 ms 24928 KB Output is correct
47 Correct 19 ms 4000 KB Output is correct
48 Correct 245 ms 28512 KB Output is correct
49 Correct 236 ms 28532 KB Output is correct
50 Correct 242 ms 28512 KB Output is correct
51 Correct 236 ms 28384 KB Output is correct
52 Correct 233 ms 27104 KB Output is correct
53 Correct 196 ms 21700 KB Output is correct
54 Correct 247 ms 29100 KB Output is correct
55 Correct 251 ms 28512 KB Output is correct
56 Correct 353 ms 28512 KB Output is correct
57 Correct 255 ms 29044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 620 KB Output is correct
6 Correct 1 ms 748 KB Output is correct
7 Correct 1 ms 620 KB Output is correct
8 Correct 1 ms 748 KB Output is correct
9 Correct 1 ms 748 KB Output is correct
10 Correct 52 ms 19700 KB Output is correct
11 Correct 64 ms 24032 KB Output is correct
12 Correct 64 ms 23648 KB Output is correct
13 Correct 66 ms 24928 KB Output is correct
14 Correct 67 ms 26464 KB Output is correct
15 Correct 61 ms 20064 KB Output is correct
16 Correct 156 ms 28000 KB Output is correct
17 Correct 154 ms 27352 KB Output is correct
18 Correct 160 ms 28768 KB Output is correct
19 Correct 275 ms 30424 KB Output is correct
20 Correct 344 ms 27300 KB Output is correct
21 Correct 358 ms 28204 KB Output is correct
22 Correct 359 ms 28040 KB Output is correct
23 Correct 334 ms 28080 KB Output is correct
24 Correct 351 ms 28124 KB Output is correct
25 Correct 350 ms 27196 KB Output is correct
26 Correct 340 ms 28000 KB Output is correct
27 Correct 350 ms 26964 KB Output is correct
28 Correct 1 ms 748 KB Output is correct
29 Correct 1 ms 748 KB Output is correct
30 Correct 1 ms 748 KB Output is correct
31 Correct 1 ms 620 KB Output is correct
32 Correct 1 ms 620 KB Output is correct
33 Correct 1 ms 748 KB Output is correct
34 Correct 1 ms 748 KB Output is correct
35 Correct 1 ms 748 KB Output is correct
36 Correct 1 ms 748 KB Output is correct
37 Correct 9 ms 3884 KB Output is correct
38 Correct 63 ms 24544 KB Output is correct
39 Correct 65 ms 24672 KB Output is correct
40 Correct 63 ms 24544 KB Output is correct
41 Correct 63 ms 24288 KB Output is correct
42 Correct 63 ms 24288 KB Output is correct
43 Correct 59 ms 22260 KB Output is correct
44 Correct 68 ms 25056 KB Output is correct
45 Correct 64 ms 24544 KB Output is correct
46 Correct 61 ms 24544 KB Output is correct
47 Correct 68 ms 24928 KB Output is correct
48 Correct 19 ms 4000 KB Output is correct
49 Correct 245 ms 28512 KB Output is correct
50 Correct 236 ms 28532 KB Output is correct
51 Correct 242 ms 28512 KB Output is correct
52 Correct 236 ms 28384 KB Output is correct
53 Correct 233 ms 27104 KB Output is correct
54 Correct 196 ms 21700 KB Output is correct
55 Correct 247 ms 29100 KB Output is correct
56 Correct 251 ms 28512 KB Output is correct
57 Correct 353 ms 28512 KB Output is correct
58 Correct 255 ms 29044 KB Output is correct
59 Correct 89 ms 9324 KB Output is correct
60 Correct 234 ms 28128 KB Output is correct
61 Correct 247 ms 27716 KB Output is correct
62 Correct 250 ms 29100 KB Output is correct
63 Correct 345 ms 30636 KB Output is correct
64 Correct 1 ms 640 KB Output is correct
65 Correct 1 ms 748 KB Output is correct
66 Correct 1 ms 748 KB Output is correct
67 Correct 2 ms 748 KB Output is correct
68 Correct 1 ms 620 KB Output is correct
69 Correct 2 ms 876 KB Output is correct
70 Correct 2 ms 876 KB Output is correct
71 Correct 2 ms 876 KB Output is correct
72 Correct 1 ms 748 KB Output is correct
73 Correct 2 ms 876 KB Output is correct
74 Runtime error 103 ms 20700 KB Execution killed with signal 11 (could be triggered by violating memory limits)
75 Halted 0 ms 0 KB -