Submission #1047182

# Submission time Handle Problem Language Result Execution time Memory
1047182 2024-08-07T10:05:55 Z KasymK Swapping Cities (APIO20_swap) C++17
100 / 100
171 ms 67452 KB
#include "bits/stdc++.h"
using namespace std;
#define ff first
#define ss second	
#define all(v) v.begin(), v.end()
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define wr puts("----------------")
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
const int maxn = 3e5+5;
const int LOG = 20;
int rep[maxn], C[maxn], deg[maxn], dp[maxn], ok[maxn], G[maxn], d[maxn], n, m, N, up[maxn][LOG];
vector<int> adj[maxn];
vector<array<int, 3>> A;

int find(int x){
    if(x == rep[x])
        return x;
    return rep[x] = find(rep[x]); // path compression
}
 
void merge(int a, int b) {
    a = find(a), b = find(b), n++;
    if(a == b)
        C[n] = 1;
    rep[n] = rep[a] = rep[b] = n;
    if(G[a] or G[b])
        G[n] = 1;
    if(C[a] or C[b])
        C[n] = 1;
    adj[n].pb(a);
    if(a != b)
        adj[n].pb(b);
}
 
void dfs(int x, int pr = -1) {
    for(int i = 1; i < LOG; ++i)
        up[x][i] = up[up[x][i-1]][i-1];
    if(ok[x])
        pr = x;
    dp[x] = pr;
    for(int &i : adj[x]) {
        d[i] = d[x]+1;
        up[i][0] = x;
        dfs(i, pr);
    }
}

int LCA(int a, int b) {
    if(d[a] < d[b])
        swap(a, b);
    int diff = d[a]-d[b];
    for(int i = LOG-1; i >= 0; --i)
        if(diff>>i&1)
            a = up[a][i];
    if(a == b)
        return a;
    for(int i = LOG-1; i >= 0; --i)
        if(up[a][i] != up[b][i])
            a = up[a][i], b = up[b][i];
    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)
        rep[i] = i;
    for(int i = 0; i < m; ++i)
        A.pb({W[i], U[i]+1, V[i]+1});
    sort(all(A));
    for(auto &[w, u, v] : A){
        deg[u]++, deg[v]++;
        if(deg[u] >= 3 or deg[v] >= 3)
            G[n+1] = 1;
        merge(u, v);
    }
    for(int i = 1; i <= n; ++i)
        ok[i] = (G[i] or C[i]);
    dfs(n);
}
 
int getMinimumFuelCapacity(int x, int y) {
    x++, y++;
    int ad = LCA(x, y);
    if(dp[ad] == -1)
        return -1;
    if(dp[ad]-N-1 <= 0)
        return -1;
    return A[dp[ad]-N-1][0];
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14940 KB Output is correct
2 Correct 2 ms 14940 KB Output is correct
3 Correct 2 ms 14940 KB Output is correct
4 Correct 2 ms 14940 KB Output is correct
5 Correct 3 ms 14940 KB Output is correct
6 Correct 2 ms 14940 KB Output is correct
7 Correct 2 ms 14940 KB Output is correct
8 Correct 2 ms 14940 KB Output is correct
9 Correct 44 ms 31956 KB Output is correct
10 Correct 68 ms 36156 KB Output is correct
11 Correct 63 ms 35780 KB Output is correct
12 Correct 62 ms 37228 KB Output is correct
13 Correct 57 ms 40904 KB Output is correct
14 Correct 61 ms 31952 KB Output is correct
15 Correct 112 ms 38032 KB Output is correct
16 Correct 109 ms 37496 KB Output is correct
17 Correct 116 ms 38892 KB Output is correct
18 Correct 147 ms 42728 KB Output is correct
19 Correct 43 ms 20756 KB Output is correct
20 Correct 97 ms 39328 KB Output is correct
21 Correct 113 ms 38784 KB Output is correct
22 Correct 104 ms 40232 KB Output is correct
23 Correct 122 ms 44076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14940 KB Output is correct
2 Correct 2 ms 14940 KB Output is correct
3 Correct 102 ms 46700 KB Output is correct
4 Correct 132 ms 48136 KB Output is correct
5 Correct 110 ms 47380 KB Output is correct
6 Correct 104 ms 47916 KB Output is correct
7 Correct 107 ms 47684 KB Output is correct
8 Correct 124 ms 46460 KB Output is correct
9 Correct 112 ms 47432 KB Output is correct
10 Correct 108 ms 46060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14940 KB Output is correct
2 Correct 2 ms 14940 KB Output is correct
3 Correct 2 ms 14940 KB Output is correct
4 Correct 2 ms 14940 KB Output is correct
5 Correct 3 ms 14940 KB Output is correct
6 Correct 2 ms 14940 KB Output is correct
7 Correct 2 ms 14940 KB Output is correct
8 Correct 2 ms 14940 KB Output is correct
9 Correct 2 ms 14936 KB Output is correct
10 Correct 2 ms 14940 KB Output is correct
11 Correct 2 ms 14940 KB Output is correct
12 Correct 2 ms 14940 KB Output is correct
13 Correct 2 ms 14940 KB Output is correct
14 Correct 2 ms 14940 KB Output is correct
15 Correct 2 ms 14940 KB Output is correct
16 Correct 2 ms 14944 KB Output is correct
17 Correct 2 ms 14940 KB Output is correct
18 Correct 2 ms 15080 KB Output is correct
19 Correct 2 ms 14940 KB Output is correct
20 Correct 2 ms 14940 KB Output is correct
21 Correct 2 ms 14940 KB Output is correct
22 Correct 2 ms 15196 KB Output is correct
23 Correct 2 ms 14940 KB Output is correct
24 Correct 2 ms 14980 KB Output is correct
25 Correct 2 ms 15196 KB Output is correct
26 Correct 3 ms 15192 KB Output is correct
27 Correct 2 ms 14952 KB Output is correct
28 Correct 2 ms 15196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14936 KB Output is correct
2 Correct 2 ms 14940 KB Output is correct
3 Correct 2 ms 14940 KB Output is correct
4 Correct 2 ms 14940 KB Output is correct
5 Correct 2 ms 14940 KB Output is correct
6 Correct 3 ms 14940 KB Output is correct
7 Correct 2 ms 14940 KB Output is correct
8 Correct 2 ms 14940 KB Output is correct
9 Correct 2 ms 14940 KB Output is correct
10 Correct 44 ms 31956 KB Output is correct
11 Correct 68 ms 36156 KB Output is correct
12 Correct 63 ms 35780 KB Output is correct
13 Correct 62 ms 37228 KB Output is correct
14 Correct 57 ms 40904 KB Output is correct
15 Correct 2 ms 14940 KB Output is correct
16 Correct 2 ms 14940 KB Output is correct
17 Correct 2 ms 14940 KB Output is correct
18 Correct 2 ms 14940 KB Output is correct
19 Correct 2 ms 14940 KB Output is correct
20 Correct 2 ms 14940 KB Output is correct
21 Correct 2 ms 14944 KB Output is correct
22 Correct 2 ms 14940 KB Output is correct
23 Correct 2 ms 15080 KB Output is correct
24 Correct 2 ms 14940 KB Output is correct
25 Correct 2 ms 14940 KB Output is correct
26 Correct 2 ms 14940 KB Output is correct
27 Correct 2 ms 15196 KB Output is correct
28 Correct 2 ms 14940 KB Output is correct
29 Correct 2 ms 14980 KB Output is correct
30 Correct 2 ms 15196 KB Output is correct
31 Correct 3 ms 15192 KB Output is correct
32 Correct 2 ms 14952 KB Output is correct
33 Correct 2 ms 15196 KB Output is correct
34 Correct 8 ms 16924 KB Output is correct
35 Correct 61 ms 37284 KB Output is correct
36 Correct 85 ms 37320 KB Output is correct
37 Correct 62 ms 37252 KB Output is correct
38 Correct 75 ms 37068 KB Output is correct
39 Correct 69 ms 37060 KB Output is correct
40 Correct 53 ms 34984 KB Output is correct
41 Correct 73 ms 37068 KB Output is correct
42 Correct 59 ms 37072 KB Output is correct
43 Correct 60 ms 42692 KB Output is correct
44 Correct 59 ms 37624 KB Output is correct
45 Correct 83 ms 50972 KB Output is correct
46 Correct 58 ms 39108 KB Output is correct
47 Correct 61 ms 39452 KB Output is correct
48 Correct 67 ms 44136 KB Output is correct
49 Correct 66 ms 53188 KB Output is correct
50 Correct 54 ms 44232 KB Output is correct
51 Correct 70 ms 48320 KB Output is correct
52 Correct 93 ms 55232 KB Output is correct
53 Correct 127 ms 58048 KB Output is correct
54 Correct 110 ms 63420 KB Output is correct
55 Correct 56 ms 43976 KB Output is correct
56 Correct 121 ms 61376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14940 KB Output is correct
2 Correct 2 ms 14940 KB Output is correct
3 Correct 2 ms 14940 KB Output is correct
4 Correct 2 ms 14940 KB Output is correct
5 Correct 3 ms 14940 KB Output is correct
6 Correct 2 ms 14940 KB Output is correct
7 Correct 2 ms 14940 KB Output is correct
8 Correct 2 ms 14940 KB Output is correct
9 Correct 44 ms 31956 KB Output is correct
10 Correct 68 ms 36156 KB Output is correct
11 Correct 63 ms 35780 KB Output is correct
12 Correct 62 ms 37228 KB Output is correct
13 Correct 57 ms 40904 KB Output is correct
14 Correct 61 ms 31952 KB Output is correct
15 Correct 112 ms 38032 KB Output is correct
16 Correct 109 ms 37496 KB Output is correct
17 Correct 116 ms 38892 KB Output is correct
18 Correct 147 ms 42728 KB Output is correct
19 Correct 102 ms 46700 KB Output is correct
20 Correct 132 ms 48136 KB Output is correct
21 Correct 110 ms 47380 KB Output is correct
22 Correct 104 ms 47916 KB Output is correct
23 Correct 107 ms 47684 KB Output is correct
24 Correct 124 ms 46460 KB Output is correct
25 Correct 112 ms 47432 KB Output is correct
26 Correct 108 ms 46060 KB Output is correct
27 Correct 2 ms 14940 KB Output is correct
28 Correct 2 ms 14940 KB Output is correct
29 Correct 2 ms 14940 KB Output is correct
30 Correct 2 ms 14940 KB Output is correct
31 Correct 2 ms 14940 KB Output is correct
32 Correct 2 ms 14940 KB Output is correct
33 Correct 2 ms 14944 KB Output is correct
34 Correct 2 ms 14940 KB Output is correct
35 Correct 2 ms 15080 KB Output is correct
36 Correct 8 ms 16924 KB Output is correct
37 Correct 61 ms 37284 KB Output is correct
38 Correct 85 ms 37320 KB Output is correct
39 Correct 62 ms 37252 KB Output is correct
40 Correct 75 ms 37068 KB Output is correct
41 Correct 69 ms 37060 KB Output is correct
42 Correct 53 ms 34984 KB Output is correct
43 Correct 73 ms 37068 KB Output is correct
44 Correct 59 ms 37072 KB Output is correct
45 Correct 60 ms 42692 KB Output is correct
46 Correct 59 ms 37624 KB Output is correct
47 Correct 12 ms 16924 KB Output is correct
48 Correct 105 ms 39680 KB Output is correct
49 Correct 107 ms 39956 KB Output is correct
50 Correct 105 ms 39984 KB Output is correct
51 Correct 102 ms 39996 KB Output is correct
52 Correct 99 ms 38552 KB Output is correct
53 Correct 89 ms 32864 KB Output is correct
54 Correct 101 ms 40240 KB Output is correct
55 Correct 106 ms 39696 KB Output is correct
56 Correct 123 ms 44072 KB Output is correct
57 Correct 116 ms 40680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14936 KB Output is correct
2 Correct 2 ms 14940 KB Output is correct
3 Correct 2 ms 14940 KB Output is correct
4 Correct 2 ms 14940 KB Output is correct
5 Correct 2 ms 14940 KB Output is correct
6 Correct 3 ms 14940 KB Output is correct
7 Correct 2 ms 14940 KB Output is correct
8 Correct 2 ms 14940 KB Output is correct
9 Correct 2 ms 14940 KB Output is correct
10 Correct 44 ms 31956 KB Output is correct
11 Correct 68 ms 36156 KB Output is correct
12 Correct 63 ms 35780 KB Output is correct
13 Correct 62 ms 37228 KB Output is correct
14 Correct 57 ms 40904 KB Output is correct
15 Correct 61 ms 31952 KB Output is correct
16 Correct 112 ms 38032 KB Output is correct
17 Correct 109 ms 37496 KB Output is correct
18 Correct 116 ms 38892 KB Output is correct
19 Correct 147 ms 42728 KB Output is correct
20 Correct 102 ms 46700 KB Output is correct
21 Correct 132 ms 48136 KB Output is correct
22 Correct 110 ms 47380 KB Output is correct
23 Correct 104 ms 47916 KB Output is correct
24 Correct 107 ms 47684 KB Output is correct
25 Correct 124 ms 46460 KB Output is correct
26 Correct 112 ms 47432 KB Output is correct
27 Correct 108 ms 46060 KB Output is correct
28 Correct 2 ms 14940 KB Output is correct
29 Correct 2 ms 14940 KB Output is correct
30 Correct 2 ms 14940 KB Output is correct
31 Correct 2 ms 14940 KB Output is correct
32 Correct 2 ms 14940 KB Output is correct
33 Correct 2 ms 14940 KB Output is correct
34 Correct 2 ms 14944 KB Output is correct
35 Correct 2 ms 14940 KB Output is correct
36 Correct 2 ms 15080 KB Output is correct
37 Correct 8 ms 16924 KB Output is correct
38 Correct 61 ms 37284 KB Output is correct
39 Correct 85 ms 37320 KB Output is correct
40 Correct 62 ms 37252 KB Output is correct
41 Correct 75 ms 37068 KB Output is correct
42 Correct 69 ms 37060 KB Output is correct
43 Correct 53 ms 34984 KB Output is correct
44 Correct 73 ms 37068 KB Output is correct
45 Correct 59 ms 37072 KB Output is correct
46 Correct 60 ms 42692 KB Output is correct
47 Correct 59 ms 37624 KB Output is correct
48 Correct 12 ms 16924 KB Output is correct
49 Correct 105 ms 39680 KB Output is correct
50 Correct 107 ms 39956 KB Output is correct
51 Correct 105 ms 39984 KB Output is correct
52 Correct 102 ms 39996 KB Output is correct
53 Correct 99 ms 38552 KB Output is correct
54 Correct 89 ms 32864 KB Output is correct
55 Correct 101 ms 40240 KB Output is correct
56 Correct 106 ms 39696 KB Output is correct
57 Correct 123 ms 44072 KB Output is correct
58 Correct 116 ms 40680 KB Output is correct
59 Correct 43 ms 20756 KB Output is correct
60 Correct 97 ms 39328 KB Output is correct
61 Correct 113 ms 38784 KB Output is correct
62 Correct 104 ms 40232 KB Output is correct
63 Correct 122 ms 44076 KB Output is correct
64 Correct 2 ms 14940 KB Output is correct
65 Correct 2 ms 14940 KB Output is correct
66 Correct 2 ms 14940 KB Output is correct
67 Correct 2 ms 15196 KB Output is correct
68 Correct 2 ms 14940 KB Output is correct
69 Correct 2 ms 14980 KB Output is correct
70 Correct 2 ms 15196 KB Output is correct
71 Correct 3 ms 15192 KB Output is correct
72 Correct 2 ms 14952 KB Output is correct
73 Correct 2 ms 15196 KB Output is correct
74 Correct 83 ms 50972 KB Output is correct
75 Correct 58 ms 39108 KB Output is correct
76 Correct 61 ms 39452 KB Output is correct
77 Correct 67 ms 44136 KB Output is correct
78 Correct 66 ms 53188 KB Output is correct
79 Correct 54 ms 44232 KB Output is correct
80 Correct 70 ms 48320 KB Output is correct
81 Correct 93 ms 55232 KB Output is correct
82 Correct 127 ms 58048 KB Output is correct
83 Correct 110 ms 63420 KB Output is correct
84 Correct 56 ms 43976 KB Output is correct
85 Correct 121 ms 61376 KB Output is correct
86 Correct 41 ms 28072 KB Output is correct
87 Correct 113 ms 44076 KB Output is correct
88 Correct 106 ms 44032 KB Output is correct
89 Correct 124 ms 47580 KB Output is correct
90 Correct 113 ms 59420 KB Output is correct
91 Correct 119 ms 57060 KB Output is correct
92 Correct 129 ms 51748 KB Output is correct
93 Correct 151 ms 59772 KB Output is correct
94 Correct 168 ms 62780 KB Output is correct
95 Correct 171 ms 67452 KB Output is correct
96 Correct 127 ms 48640 KB Output is correct
97 Correct 148 ms 55936 KB Output is correct