Submission #1066868

# Submission time Handle Problem Language Result Execution time Memory
1066868 2024-08-20T08:20:50 Z vjudge1 Swapping Cities (APIO20_swap) C++17
100 / 100
239 ms 69812 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;
        C[i] = deg[i] = G[i] = 0;
    }
 
    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 3 ms 19036 KB Output is correct
2 Correct 3 ms 19036 KB Output is correct
3 Correct 2 ms 19064 KB Output is correct
4 Correct 5 ms 19032 KB Output is correct
5 Correct 3 ms 19036 KB Output is correct
6 Correct 3 ms 19036 KB Output is correct
7 Correct 3 ms 19036 KB Output is correct
8 Correct 4 ms 19036 KB Output is correct
9 Correct 54 ms 37064 KB Output is correct
10 Correct 70 ms 41416 KB Output is correct
11 Correct 58 ms 40904 KB Output is correct
12 Correct 68 ms 42352 KB Output is correct
13 Correct 64 ms 46276 KB Output is correct
14 Correct 54 ms 37320 KB Output is correct
15 Correct 118 ms 45788 KB Output is correct
16 Correct 115 ms 45044 KB Output is correct
17 Correct 114 ms 46452 KB Output is correct
18 Correct 165 ms 50508 KB Output is correct
19 Correct 54 ms 26644 KB Output is correct
20 Correct 126 ms 46916 KB Output is correct
21 Correct 114 ms 46208 KB Output is correct
22 Correct 127 ms 47956 KB Output is correct
23 Correct 145 ms 51756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 19036 KB Output is correct
2 Correct 3 ms 19036 KB Output is correct
3 Correct 126 ms 53356 KB Output is correct
4 Correct 130 ms 54860 KB Output is correct
5 Correct 130 ms 54236 KB Output is correct
6 Correct 126 ms 54496 KB Output is correct
7 Correct 140 ms 54600 KB Output is correct
8 Correct 120 ms 53116 KB Output is correct
9 Correct 126 ms 54084 KB Output is correct
10 Correct 124 ms 52812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 19036 KB Output is correct
2 Correct 3 ms 19036 KB Output is correct
3 Correct 2 ms 19064 KB Output is correct
4 Correct 5 ms 19032 KB Output is correct
5 Correct 3 ms 19036 KB Output is correct
6 Correct 3 ms 19036 KB Output is correct
7 Correct 3 ms 19036 KB Output is correct
8 Correct 4 ms 19036 KB Output is correct
9 Correct 3 ms 19032 KB Output is correct
10 Correct 2 ms 19072 KB Output is correct
11 Correct 3 ms 19084 KB Output is correct
12 Correct 3 ms 19036 KB Output is correct
13 Correct 3 ms 19072 KB Output is correct
14 Correct 3 ms 19032 KB Output is correct
15 Correct 3 ms 19032 KB Output is correct
16 Correct 3 ms 19036 KB Output is correct
17 Correct 3 ms 19036 KB Output is correct
18 Correct 3 ms 19036 KB Output is correct
19 Correct 3 ms 19036 KB Output is correct
20 Correct 3 ms 19080 KB Output is correct
21 Correct 3 ms 19104 KB Output is correct
22 Correct 5 ms 19328 KB Output is correct
23 Correct 3 ms 19036 KB Output is correct
24 Correct 4 ms 19036 KB Output is correct
25 Correct 4 ms 19292 KB Output is correct
26 Correct 3 ms 19292 KB Output is correct
27 Correct 3 ms 19076 KB Output is correct
28 Correct 3 ms 19348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 19032 KB Output is correct
2 Correct 3 ms 19036 KB Output is correct
3 Correct 3 ms 19036 KB Output is correct
4 Correct 2 ms 19064 KB Output is correct
5 Correct 5 ms 19032 KB Output is correct
6 Correct 3 ms 19036 KB Output is correct
7 Correct 3 ms 19036 KB Output is correct
8 Correct 3 ms 19036 KB Output is correct
9 Correct 4 ms 19036 KB Output is correct
10 Correct 54 ms 37064 KB Output is correct
11 Correct 70 ms 41416 KB Output is correct
12 Correct 58 ms 40904 KB Output is correct
13 Correct 68 ms 42352 KB Output is correct
14 Correct 64 ms 46276 KB Output is correct
15 Correct 2 ms 19072 KB Output is correct
16 Correct 3 ms 19084 KB Output is correct
17 Correct 3 ms 19036 KB Output is correct
18 Correct 3 ms 19072 KB Output is correct
19 Correct 3 ms 19032 KB Output is correct
20 Correct 3 ms 19032 KB Output is correct
21 Correct 3 ms 19036 KB Output is correct
22 Correct 3 ms 19036 KB Output is correct
23 Correct 3 ms 19036 KB Output is correct
24 Correct 3 ms 19036 KB Output is correct
25 Correct 3 ms 19080 KB Output is correct
26 Correct 3 ms 19104 KB Output is correct
27 Correct 5 ms 19328 KB Output is correct
28 Correct 3 ms 19036 KB Output is correct
29 Correct 4 ms 19036 KB Output is correct
30 Correct 4 ms 19292 KB Output is correct
31 Correct 3 ms 19292 KB Output is correct
32 Correct 3 ms 19076 KB Output is correct
33 Correct 3 ms 19348 KB Output is correct
34 Correct 10 ms 21016 KB Output is correct
35 Correct 69 ms 42224 KB Output is correct
36 Correct 74 ms 42088 KB Output is correct
37 Correct 65 ms 42100 KB Output is correct
38 Correct 66 ms 41760 KB Output is correct
39 Correct 63 ms 41776 KB Output is correct
40 Correct 56 ms 39700 KB Output is correct
41 Correct 64 ms 42448 KB Output is correct
42 Correct 66 ms 42184 KB Output is correct
43 Correct 64 ms 47244 KB Output is correct
44 Correct 76 ms 42440 KB Output is correct
45 Correct 100 ms 55992 KB Output is correct
46 Correct 64 ms 42188 KB Output is correct
47 Correct 73 ms 42384 KB Output is correct
48 Correct 78 ms 46836 KB Output is correct
49 Correct 89 ms 55760 KB Output is correct
50 Correct 92 ms 47196 KB Output is correct
51 Correct 96 ms 50884 KB Output is correct
52 Correct 115 ms 57592 KB Output is correct
53 Correct 154 ms 58052 KB Output is correct
54 Correct 168 ms 65220 KB Output is correct
55 Correct 79 ms 46280 KB Output is correct
56 Correct 142 ms 62652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 19036 KB Output is correct
2 Correct 3 ms 19036 KB Output is correct
3 Correct 2 ms 19064 KB Output is correct
4 Correct 5 ms 19032 KB Output is correct
5 Correct 3 ms 19036 KB Output is correct
6 Correct 3 ms 19036 KB Output is correct
7 Correct 3 ms 19036 KB Output is correct
8 Correct 4 ms 19036 KB Output is correct
9 Correct 54 ms 37064 KB Output is correct
10 Correct 70 ms 41416 KB Output is correct
11 Correct 58 ms 40904 KB Output is correct
12 Correct 68 ms 42352 KB Output is correct
13 Correct 64 ms 46276 KB Output is correct
14 Correct 54 ms 37320 KB Output is correct
15 Correct 118 ms 45788 KB Output is correct
16 Correct 115 ms 45044 KB Output is correct
17 Correct 114 ms 46452 KB Output is correct
18 Correct 165 ms 50508 KB Output is correct
19 Correct 126 ms 53356 KB Output is correct
20 Correct 130 ms 54860 KB Output is correct
21 Correct 130 ms 54236 KB Output is correct
22 Correct 126 ms 54496 KB Output is correct
23 Correct 140 ms 54600 KB Output is correct
24 Correct 120 ms 53116 KB Output is correct
25 Correct 126 ms 54084 KB Output is correct
26 Correct 124 ms 52812 KB Output is correct
27 Correct 2 ms 19072 KB Output is correct
28 Correct 3 ms 19084 KB Output is correct
29 Correct 3 ms 19036 KB Output is correct
30 Correct 3 ms 19072 KB Output is correct
31 Correct 3 ms 19032 KB Output is correct
32 Correct 3 ms 19032 KB Output is correct
33 Correct 3 ms 19036 KB Output is correct
34 Correct 3 ms 19036 KB Output is correct
35 Correct 3 ms 19036 KB Output is correct
36 Correct 10 ms 21016 KB Output is correct
37 Correct 69 ms 42224 KB Output is correct
38 Correct 74 ms 42088 KB Output is correct
39 Correct 65 ms 42100 KB Output is correct
40 Correct 66 ms 41760 KB Output is correct
41 Correct 63 ms 41776 KB Output is correct
42 Correct 56 ms 39700 KB Output is correct
43 Correct 64 ms 42448 KB Output is correct
44 Correct 66 ms 42184 KB Output is correct
45 Correct 64 ms 47244 KB Output is correct
46 Correct 76 ms 42440 KB Output is correct
47 Correct 15 ms 21276 KB Output is correct
48 Correct 161 ms 42796 KB Output is correct
49 Correct 129 ms 47068 KB Output is correct
50 Correct 182 ms 43308 KB Output is correct
51 Correct 165 ms 46320 KB Output is correct
52 Correct 172 ms 45532 KB Output is correct
53 Correct 120 ms 33724 KB Output is correct
54 Correct 159 ms 47792 KB Output is correct
55 Correct 145 ms 46984 KB Output is correct
56 Correct 175 ms 50992 KB Output is correct
57 Correct 148 ms 47964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 19032 KB Output is correct
2 Correct 3 ms 19036 KB Output is correct
3 Correct 3 ms 19036 KB Output is correct
4 Correct 2 ms 19064 KB Output is correct
5 Correct 5 ms 19032 KB Output is correct
6 Correct 3 ms 19036 KB Output is correct
7 Correct 3 ms 19036 KB Output is correct
8 Correct 3 ms 19036 KB Output is correct
9 Correct 4 ms 19036 KB Output is correct
10 Correct 54 ms 37064 KB Output is correct
11 Correct 70 ms 41416 KB Output is correct
12 Correct 58 ms 40904 KB Output is correct
13 Correct 68 ms 42352 KB Output is correct
14 Correct 64 ms 46276 KB Output is correct
15 Correct 54 ms 37320 KB Output is correct
16 Correct 118 ms 45788 KB Output is correct
17 Correct 115 ms 45044 KB Output is correct
18 Correct 114 ms 46452 KB Output is correct
19 Correct 165 ms 50508 KB Output is correct
20 Correct 126 ms 53356 KB Output is correct
21 Correct 130 ms 54860 KB Output is correct
22 Correct 130 ms 54236 KB Output is correct
23 Correct 126 ms 54496 KB Output is correct
24 Correct 140 ms 54600 KB Output is correct
25 Correct 120 ms 53116 KB Output is correct
26 Correct 126 ms 54084 KB Output is correct
27 Correct 124 ms 52812 KB Output is correct
28 Correct 2 ms 19072 KB Output is correct
29 Correct 3 ms 19084 KB Output is correct
30 Correct 3 ms 19036 KB Output is correct
31 Correct 3 ms 19072 KB Output is correct
32 Correct 3 ms 19032 KB Output is correct
33 Correct 3 ms 19032 KB Output is correct
34 Correct 3 ms 19036 KB Output is correct
35 Correct 3 ms 19036 KB Output is correct
36 Correct 3 ms 19036 KB Output is correct
37 Correct 10 ms 21016 KB Output is correct
38 Correct 69 ms 42224 KB Output is correct
39 Correct 74 ms 42088 KB Output is correct
40 Correct 65 ms 42100 KB Output is correct
41 Correct 66 ms 41760 KB Output is correct
42 Correct 63 ms 41776 KB Output is correct
43 Correct 56 ms 39700 KB Output is correct
44 Correct 64 ms 42448 KB Output is correct
45 Correct 66 ms 42184 KB Output is correct
46 Correct 64 ms 47244 KB Output is correct
47 Correct 76 ms 42440 KB Output is correct
48 Correct 15 ms 21276 KB Output is correct
49 Correct 161 ms 42796 KB Output is correct
50 Correct 129 ms 47068 KB Output is correct
51 Correct 182 ms 43308 KB Output is correct
52 Correct 165 ms 46320 KB Output is correct
53 Correct 172 ms 45532 KB Output is correct
54 Correct 120 ms 33724 KB Output is correct
55 Correct 159 ms 47792 KB Output is correct
56 Correct 145 ms 46984 KB Output is correct
57 Correct 175 ms 50992 KB Output is correct
58 Correct 148 ms 47964 KB Output is correct
59 Correct 54 ms 26644 KB Output is correct
60 Correct 126 ms 46916 KB Output is correct
61 Correct 114 ms 46208 KB Output is correct
62 Correct 127 ms 47956 KB Output is correct
63 Correct 145 ms 51756 KB Output is correct
64 Correct 3 ms 19036 KB Output is correct
65 Correct 3 ms 19080 KB Output is correct
66 Correct 3 ms 19104 KB Output is correct
67 Correct 5 ms 19328 KB Output is correct
68 Correct 3 ms 19036 KB Output is correct
69 Correct 4 ms 19036 KB Output is correct
70 Correct 4 ms 19292 KB Output is correct
71 Correct 3 ms 19292 KB Output is correct
72 Correct 3 ms 19076 KB Output is correct
73 Correct 3 ms 19348 KB Output is correct
74 Correct 100 ms 55992 KB Output is correct
75 Correct 64 ms 42188 KB Output is correct
76 Correct 73 ms 42384 KB Output is correct
77 Correct 78 ms 46836 KB Output is correct
78 Correct 89 ms 55760 KB Output is correct
79 Correct 92 ms 47196 KB Output is correct
80 Correct 96 ms 50884 KB Output is correct
81 Correct 115 ms 57592 KB Output is correct
82 Correct 154 ms 58052 KB Output is correct
83 Correct 168 ms 65220 KB Output is correct
84 Correct 79 ms 46280 KB Output is correct
85 Correct 142 ms 62652 KB Output is correct
86 Correct 67 ms 31716 KB Output is correct
87 Correct 154 ms 47008 KB Output is correct
88 Correct 126 ms 47076 KB Output is correct
89 Correct 182 ms 45992 KB Output is correct
90 Correct 141 ms 62012 KB Output is correct
91 Correct 189 ms 53128 KB Output is correct
92 Correct 184 ms 53684 KB Output is correct
93 Correct 239 ms 61312 KB Output is correct
94 Correct 231 ms 62696 KB Output is correct
95 Correct 222 ms 69812 KB Output is correct
96 Correct 177 ms 51500 KB Output is correct
97 Correct 224 ms 54976 KB Output is correct