Submission #1043824

# Submission time Handle Problem Language Result Execution time Memory
1043824 2024-08-04T19:56:52 Z VMaksimoski008 Swapping Cities (APIO20_swap) C++17
100 / 100
171 ms 62148 KB
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;

const int maxn = 3e5 + 5;
const int LOG = 20;

int par[maxn], C[maxn], deg[maxn], dp[maxn], P[maxn], ok[maxn], G[maxn], d[maxn], n, m, N, up[maxn][LOG];
vector<int> graph[maxn];
vector<array<int, 3> > edges;

int find(int u) {
    if(u == par[u]) return u;
    return par[u] = find(par[u]);
}

void uni(int a, int b) {
    a = find(a); b = find(b); n++;
    if(a == b) C[n] = 1;
    par[n] = par[a] = par[b] = n;
    if(G[a] || G[b]) G[n] = 1;
    if(C[a] || C[b]) C[n] = 1;
    graph[n].push_back(a);
    if(a != b) graph[n].push_back(b);
}

void dfs(int u, int last) {
    for(int i=1; i<LOG; i++) up[u][i] = up[up[u][i-1]][i-1];
    if(ok[u]) last = u;
    dp[u] = last;

    for(int &v : graph[u]) {
        d[v] = d[u] + 1;
        up[v][0] = u;
        P[v] = u;
        dfs(v, last);
    }
}

int get_lca(int a, int b) {
    if(d[a] < d[b]) swap(a, b);
    int D = d[a] - d[b];
    for(int j=LOG-1; j>=0; j--)
        if(D & (1 << j)) a = up[a][j];

    if(a == b) return a;

    for(int j=LOG-1; j>=0; j--)
        if(up[a][j] != up[b][j]) a = up[a][j], b = up[b][j];
    return up[a][0];
}

void init(int _N, int _M, vector<int> U, vector<int> V, vector<int> W) {
    n = _N; N = _N; m = _M;
    for(int i=1; i<=_N+_M; i++) par[i] = i;

    for(int i=0; i<m; i++) edges.push_back({ W[i], U[i] + 1, V[i] + 1});
    sort(edges.begin(), edges.end());
    for(auto &[w, u, v] : edges) {
        deg[u]++; deg[v]++;
        if(deg[u] >= 3 || deg[v] >= 3) G[n+1] = 1;
        uni(u, v);
    }

    for(int i=1; i<=n; i++) ok[i] = (G[i] || C[i]);
    dfs(n, -1);
}

int getMinimumFuelCapacity(int X, int Y) {
    int L = get_lca(X+1, Y+1);
    if(dp[L] == -1) return -1;
    return edges[dp[L]-N-1][0];
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16988 KB Output is correct
2 Correct 2 ms 16988 KB Output is correct
3 Correct 2 ms 16988 KB Output is correct
4 Correct 2 ms 16988 KB Output is correct
5 Correct 2 ms 16988 KB Output is correct
6 Correct 3 ms 16988 KB Output is correct
7 Correct 2 ms 16988 KB Output is correct
8 Correct 3 ms 16988 KB Output is correct
9 Correct 45 ms 33508 KB Output is correct
10 Correct 62 ms 37724 KB Output is correct
11 Correct 59 ms 37468 KB Output is correct
12 Correct 66 ms 38856 KB Output is correct
13 Correct 59 ms 42768 KB Output is correct
14 Correct 47 ms 33748 KB Output is correct
15 Correct 103 ms 39764 KB Output is correct
16 Correct 106 ms 39148 KB Output is correct
17 Correct 110 ms 40428 KB Output is correct
18 Correct 136 ms 44476 KB Output is correct
19 Correct 44 ms 22804 KB Output is correct
20 Correct 114 ms 41068 KB Output is correct
21 Correct 102 ms 40320 KB Output is correct
22 Correct 107 ms 42000 KB Output is correct
23 Correct 130 ms 45868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16988 KB Output is correct
2 Correct 2 ms 16988 KB Output is correct
3 Correct 106 ms 48228 KB Output is correct
4 Correct 113 ms 49664 KB Output is correct
5 Correct 112 ms 48872 KB Output is correct
6 Correct 109 ms 49456 KB Output is correct
7 Correct 114 ms 49476 KB Output is correct
8 Correct 129 ms 47988 KB Output is correct
9 Correct 108 ms 48964 KB Output is correct
10 Correct 108 ms 47744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16988 KB Output is correct
2 Correct 2 ms 16988 KB Output is correct
3 Correct 2 ms 16988 KB Output is correct
4 Correct 2 ms 16988 KB Output is correct
5 Correct 2 ms 16988 KB Output is correct
6 Correct 3 ms 16988 KB Output is correct
7 Correct 2 ms 16988 KB Output is correct
8 Correct 3 ms 16988 KB Output is correct
9 Correct 2 ms 17240 KB Output is correct
10 Correct 3 ms 16984 KB Output is correct
11 Correct 2 ms 16988 KB Output is correct
12 Correct 3 ms 16988 KB Output is correct
13 Correct 2 ms 16988 KB Output is correct
14 Correct 3 ms 16984 KB Output is correct
15 Correct 2 ms 16988 KB Output is correct
16 Correct 3 ms 16988 KB Output is correct
17 Correct 2 ms 16988 KB Output is correct
18 Correct 2 ms 16988 KB Output is correct
19 Correct 2 ms 16988 KB Output is correct
20 Correct 3 ms 16984 KB Output is correct
21 Correct 3 ms 16988 KB Output is correct
22 Correct 2 ms 17124 KB Output is correct
23 Correct 2 ms 17108 KB Output is correct
24 Correct 3 ms 16988 KB Output is correct
25 Correct 3 ms 16988 KB Output is correct
26 Correct 3 ms 17244 KB Output is correct
27 Correct 2 ms 16988 KB Output is correct
28 Correct 3 ms 17244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 17240 KB Output is correct
2 Correct 2 ms 16988 KB Output is correct
3 Correct 2 ms 16988 KB Output is correct
4 Correct 2 ms 16988 KB Output is correct
5 Correct 2 ms 16988 KB Output is correct
6 Correct 2 ms 16988 KB Output is correct
7 Correct 3 ms 16988 KB Output is correct
8 Correct 2 ms 16988 KB Output is correct
9 Correct 3 ms 16988 KB Output is correct
10 Correct 45 ms 33508 KB Output is correct
11 Correct 62 ms 37724 KB Output is correct
12 Correct 59 ms 37468 KB Output is correct
13 Correct 66 ms 38856 KB Output is correct
14 Correct 59 ms 42768 KB Output is correct
15 Correct 3 ms 16984 KB Output is correct
16 Correct 2 ms 16988 KB Output is correct
17 Correct 3 ms 16988 KB Output is correct
18 Correct 2 ms 16988 KB Output is correct
19 Correct 3 ms 16984 KB Output is correct
20 Correct 2 ms 16988 KB Output is correct
21 Correct 3 ms 16988 KB Output is correct
22 Correct 2 ms 16988 KB Output is correct
23 Correct 2 ms 16988 KB Output is correct
24 Correct 2 ms 16988 KB Output is correct
25 Correct 3 ms 16984 KB Output is correct
26 Correct 3 ms 16988 KB Output is correct
27 Correct 2 ms 17124 KB Output is correct
28 Correct 2 ms 17108 KB Output is correct
29 Correct 3 ms 16988 KB Output is correct
30 Correct 3 ms 16988 KB Output is correct
31 Correct 3 ms 17244 KB Output is correct
32 Correct 2 ms 16988 KB Output is correct
33 Correct 3 ms 17244 KB Output is correct
34 Correct 10 ms 18716 KB Output is correct
35 Correct 63 ms 38920 KB Output is correct
36 Correct 72 ms 38856 KB Output is correct
37 Correct 61 ms 39108 KB Output is correct
38 Correct 64 ms 38772 KB Output is correct
39 Correct 61 ms 38596 KB Output is correct
40 Correct 51 ms 36804 KB Output is correct
41 Correct 62 ms 38852 KB Output is correct
42 Correct 55 ms 38852 KB Output is correct
43 Correct 55 ms 44236 KB Output is correct
44 Correct 61 ms 39252 KB Output is correct
45 Correct 91 ms 51904 KB Output is correct
46 Correct 64 ms 38864 KB Output is correct
47 Correct 55 ms 39364 KB Output is correct
48 Correct 69 ms 43592 KB Output is correct
49 Correct 68 ms 52160 KB Output is correct
50 Correct 53 ms 43460 KB Output is correct
51 Correct 72 ms 47044 KB Output is correct
52 Correct 97 ms 53960 KB Output is correct
53 Correct 122 ms 55492 KB Output is correct
54 Correct 110 ms 60608 KB Output is correct
55 Correct 68 ms 43716 KB Output is correct
56 Correct 103 ms 58816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16988 KB Output is correct
2 Correct 2 ms 16988 KB Output is correct
3 Correct 2 ms 16988 KB Output is correct
4 Correct 2 ms 16988 KB Output is correct
5 Correct 2 ms 16988 KB Output is correct
6 Correct 3 ms 16988 KB Output is correct
7 Correct 2 ms 16988 KB Output is correct
8 Correct 3 ms 16988 KB Output is correct
9 Correct 45 ms 33508 KB Output is correct
10 Correct 62 ms 37724 KB Output is correct
11 Correct 59 ms 37468 KB Output is correct
12 Correct 66 ms 38856 KB Output is correct
13 Correct 59 ms 42768 KB Output is correct
14 Correct 47 ms 33748 KB Output is correct
15 Correct 103 ms 39764 KB Output is correct
16 Correct 106 ms 39148 KB Output is correct
17 Correct 110 ms 40428 KB Output is correct
18 Correct 136 ms 44476 KB Output is correct
19 Correct 106 ms 48228 KB Output is correct
20 Correct 113 ms 49664 KB Output is correct
21 Correct 112 ms 48872 KB Output is correct
22 Correct 109 ms 49456 KB Output is correct
23 Correct 114 ms 49476 KB Output is correct
24 Correct 129 ms 47988 KB Output is correct
25 Correct 108 ms 48964 KB Output is correct
26 Correct 108 ms 47744 KB Output is correct
27 Correct 3 ms 16984 KB Output is correct
28 Correct 2 ms 16988 KB Output is correct
29 Correct 3 ms 16988 KB Output is correct
30 Correct 2 ms 16988 KB Output is correct
31 Correct 3 ms 16984 KB Output is correct
32 Correct 2 ms 16988 KB Output is correct
33 Correct 3 ms 16988 KB Output is correct
34 Correct 2 ms 16988 KB Output is correct
35 Correct 2 ms 16988 KB Output is correct
36 Correct 10 ms 18716 KB Output is correct
37 Correct 63 ms 38920 KB Output is correct
38 Correct 72 ms 38856 KB Output is correct
39 Correct 61 ms 39108 KB Output is correct
40 Correct 64 ms 38772 KB Output is correct
41 Correct 61 ms 38596 KB Output is correct
42 Correct 51 ms 36804 KB Output is correct
43 Correct 62 ms 38852 KB Output is correct
44 Correct 55 ms 38852 KB Output is correct
45 Correct 55 ms 44236 KB Output is correct
46 Correct 61 ms 39252 KB Output is correct
47 Correct 17 ms 18972 KB Output is correct
48 Correct 111 ms 41364 KB Output is correct
49 Correct 111 ms 41512 KB Output is correct
50 Correct 117 ms 41660 KB Output is correct
51 Correct 99 ms 41528 KB Output is correct
52 Correct 101 ms 40052 KB Output is correct
53 Correct 101 ms 34580 KB Output is correct
54 Correct 108 ms 41932 KB Output is correct
55 Correct 101 ms 41232 KB Output is correct
56 Correct 136 ms 45840 KB Output is correct
57 Correct 130 ms 42284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 17240 KB Output is correct
2 Correct 2 ms 16988 KB Output is correct
3 Correct 2 ms 16988 KB Output is correct
4 Correct 2 ms 16988 KB Output is correct
5 Correct 2 ms 16988 KB Output is correct
6 Correct 2 ms 16988 KB Output is correct
7 Correct 3 ms 16988 KB Output is correct
8 Correct 2 ms 16988 KB Output is correct
9 Correct 3 ms 16988 KB Output is correct
10 Correct 45 ms 33508 KB Output is correct
11 Correct 62 ms 37724 KB Output is correct
12 Correct 59 ms 37468 KB Output is correct
13 Correct 66 ms 38856 KB Output is correct
14 Correct 59 ms 42768 KB Output is correct
15 Correct 47 ms 33748 KB Output is correct
16 Correct 103 ms 39764 KB Output is correct
17 Correct 106 ms 39148 KB Output is correct
18 Correct 110 ms 40428 KB Output is correct
19 Correct 136 ms 44476 KB Output is correct
20 Correct 106 ms 48228 KB Output is correct
21 Correct 113 ms 49664 KB Output is correct
22 Correct 112 ms 48872 KB Output is correct
23 Correct 109 ms 49456 KB Output is correct
24 Correct 114 ms 49476 KB Output is correct
25 Correct 129 ms 47988 KB Output is correct
26 Correct 108 ms 48964 KB Output is correct
27 Correct 108 ms 47744 KB Output is correct
28 Correct 3 ms 16984 KB Output is correct
29 Correct 2 ms 16988 KB Output is correct
30 Correct 3 ms 16988 KB Output is correct
31 Correct 2 ms 16988 KB Output is correct
32 Correct 3 ms 16984 KB Output is correct
33 Correct 2 ms 16988 KB Output is correct
34 Correct 3 ms 16988 KB Output is correct
35 Correct 2 ms 16988 KB Output is correct
36 Correct 2 ms 16988 KB Output is correct
37 Correct 10 ms 18716 KB Output is correct
38 Correct 63 ms 38920 KB Output is correct
39 Correct 72 ms 38856 KB Output is correct
40 Correct 61 ms 39108 KB Output is correct
41 Correct 64 ms 38772 KB Output is correct
42 Correct 61 ms 38596 KB Output is correct
43 Correct 51 ms 36804 KB Output is correct
44 Correct 62 ms 38852 KB Output is correct
45 Correct 55 ms 38852 KB Output is correct
46 Correct 55 ms 44236 KB Output is correct
47 Correct 61 ms 39252 KB Output is correct
48 Correct 17 ms 18972 KB Output is correct
49 Correct 111 ms 41364 KB Output is correct
50 Correct 111 ms 41512 KB Output is correct
51 Correct 117 ms 41660 KB Output is correct
52 Correct 99 ms 41528 KB Output is correct
53 Correct 101 ms 40052 KB Output is correct
54 Correct 101 ms 34580 KB Output is correct
55 Correct 108 ms 41932 KB Output is correct
56 Correct 101 ms 41232 KB Output is correct
57 Correct 136 ms 45840 KB Output is correct
58 Correct 130 ms 42284 KB Output is correct
59 Correct 44 ms 22804 KB Output is correct
60 Correct 114 ms 41068 KB Output is correct
61 Correct 102 ms 40320 KB Output is correct
62 Correct 107 ms 42000 KB Output is correct
63 Correct 130 ms 45868 KB Output is correct
64 Correct 2 ms 16988 KB Output is correct
65 Correct 3 ms 16984 KB Output is correct
66 Correct 3 ms 16988 KB Output is correct
67 Correct 2 ms 17124 KB Output is correct
68 Correct 2 ms 17108 KB Output is correct
69 Correct 3 ms 16988 KB Output is correct
70 Correct 3 ms 16988 KB Output is correct
71 Correct 3 ms 17244 KB Output is correct
72 Correct 2 ms 16988 KB Output is correct
73 Correct 3 ms 17244 KB Output is correct
74 Correct 91 ms 51904 KB Output is correct
75 Correct 64 ms 38864 KB Output is correct
76 Correct 55 ms 39364 KB Output is correct
77 Correct 69 ms 43592 KB Output is correct
78 Correct 68 ms 52160 KB Output is correct
79 Correct 53 ms 43460 KB Output is correct
80 Correct 72 ms 47044 KB Output is correct
81 Correct 97 ms 53960 KB Output is correct
82 Correct 122 ms 55492 KB Output is correct
83 Correct 110 ms 60608 KB Output is correct
84 Correct 68 ms 43716 KB Output is correct
85 Correct 103 ms 58816 KB Output is correct
86 Correct 47 ms 28184 KB Output is correct
87 Correct 123 ms 41520 KB Output is correct
88 Correct 111 ms 41516 KB Output is correct
89 Correct 130 ms 45068 KB Output is correct
90 Correct 120 ms 56328 KB Output is correct
91 Correct 121 ms 52704 KB Output is correct
92 Correct 137 ms 48532 KB Output is correct
93 Correct 155 ms 55048 KB Output is correct
94 Correct 171 ms 57920 KB Output is correct
95 Correct 168 ms 62148 KB Output is correct
96 Correct 146 ms 46124 KB Output is correct
97 Correct 150 ms 52328 KB Output is correct