Submission #676527

# Submission time Handle Problem Language Result Execution time Memory
676527 2022-12-31T07:35:07 Z fatemetmhr Swapping Cities (APIO20_swap) C++17
100 / 100
335 ms 57060 KB
// ~~ Be name khoda ~~

#include "swap.h"
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define all(x) x.begin(), x.end()
#define pb     push_back
#define fi     first
#define se     second

const int maxn5 = 3e5 + 10;
const int lg    = 21;


int parlca[lg][maxn5], par[maxn5], len[maxn5];
int h[maxn5], ans[maxn5], sar[maxn5], tah[maxn5];
bool masir[maxn5], allno = false;
vector <int> adj[maxn5];
vector <pair<int, int>> ed;


int get_par(int a){return par[a] == -1 ? a : par[a] = get_par(par[a]);}

void dfs(int v){
    if(!masir[v])
        ans[v] = len[v];
    for(int i = 1; i < lg && parlca[i - 1][v] != -1; i++)
        parlca[i][v] = parlca[i - 1][parlca[i - 1][v]];
    for(auto u : adj[v]){
        ans[u] = ans[v];
        parlca[0][u] = v;
        h[u] = h[v] + 1;
        dfs(u);
    }
}

inline int lca(int a, int b){
    if(h[a] < h[b])
        swap(a, b);
    int d = h[a] - h[b];
    for(int i = 0; i < lg; i++) if((d >> i)&1)
        a = parlca[i][a];
    if(a == b)
        return a;
    for(int i = lg - 1; i >= 0; i--) if(parlca[i][a] != parlca[i][b]){
        a = parlca[i][a];
        b = parlca[i][b];
    }
    return parlca[0][a];
}

void init(int n, int m, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
    for(int i = 0; i < m; i++)
        ed.pb({W[i], i});
    sort(all(ed));
    memset(par, -1, sizeof par);
    int newnode = n;
    memset(masir, true, sizeof masir);
    for(int i = 0; i < n; i++)
        sar[i] = tah[i] = i;
    for(auto [w, id] : ed){
        int u = get_par(U[id]), v = get_par(V[id]);
        //cout << "ok " << u << ' ' << v << ' ' << U[id] << ' ' << V[id] << ' ' << w << endl;
        if(u == v){
            if(masir[u]){
                masir[u] = false;
                len[u] = w;
            }
            continue;
        }
        par[u] = newnode;
        par[v] = newnode;
        len[newnode] = w;
        masir[newnode] = (masir[u] && masir[v]);
        if(masir[newnode]){
            bool re1 = U[id] == sar[u] || U[id] == tah[u];
            bool re2 = V[id] == sar[v] || V[id] == tah[v];
            masir[newnode] = re1 && re2;
            if(masir[newnode]){
                sar[newnode] = U[id] == sar[u] ? tah[u] : sar[u];
                tah[newnode] = V[id] == sar[v] ? tah[v] : sar[v];
            }
        }
        adj[newnode].pb(u);
        adj[newnode].pb(v);
        //cout << "ya merged " << newnode << ' ' << masir[newnode] << ' ' << sar[newnode] << ' ' << tah[newnode] << endl;
        newnode++;
    }
    if(masir[newnode - 1]){
        allno = true;
        return;
    }
    memset(parlca, -1, sizeof parlca);
    dfs(newnode - 1);
}

int getMinimumFuelCapacity(int x, int y){
    if(allno)
        return -1;
    int c = lca(x, y);
    return max({len[c], ans[x], ans[y]});
}

// hen?
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8788 KB Output is correct
2 Correct 5 ms 8788 KB Output is correct
3 Correct 6 ms 8788 KB Output is correct
4 Correct 5 ms 8768 KB Output is correct
5 Correct 5 ms 8916 KB Output is correct
6 Correct 5 ms 8916 KB Output is correct
7 Correct 7 ms 8920 KB Output is correct
8 Correct 5 ms 8916 KB Output is correct
9 Correct 49 ms 16836 KB Output is correct
10 Correct 61 ms 18692 KB Output is correct
11 Correct 59 ms 18564 KB Output is correct
12 Correct 61 ms 19124 KB Output is correct
13 Correct 55 ms 19636 KB Output is correct
14 Correct 71 ms 17212 KB Output is correct
15 Correct 113 ms 22836 KB Output is correct
16 Correct 109 ms 22588 KB Output is correct
17 Correct 119 ms 23204 KB Output is correct
18 Correct 110 ms 23832 KB Output is correct
19 Correct 99 ms 40388 KB Output is correct
20 Correct 236 ms 50060 KB Output is correct
21 Correct 221 ms 50044 KB Output is correct
22 Correct 259 ms 50896 KB Output is correct
23 Correct 304 ms 54684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8788 KB Output is correct
2 Correct 5 ms 8788 KB Output is correct
3 Correct 292 ms 56156 KB Output is correct
4 Correct 307 ms 56996 KB Output is correct
5 Correct 329 ms 56784 KB Output is correct
6 Correct 317 ms 56752 KB Output is correct
7 Correct 289 ms 57060 KB Output is correct
8 Correct 285 ms 56188 KB Output is correct
9 Correct 296 ms 56668 KB Output is correct
10 Correct 292 ms 56172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8788 KB Output is correct
2 Correct 5 ms 8788 KB Output is correct
3 Correct 6 ms 8788 KB Output is correct
4 Correct 5 ms 8768 KB Output is correct
5 Correct 5 ms 8916 KB Output is correct
6 Correct 5 ms 8916 KB Output is correct
7 Correct 7 ms 8920 KB Output is correct
8 Correct 5 ms 8916 KB Output is correct
9 Correct 14 ms 33492 KB Output is correct
10 Correct 14 ms 33620 KB Output is correct
11 Correct 14 ms 33492 KB Output is correct
12 Correct 16 ms 33556 KB Output is correct
13 Correct 14 ms 33596 KB Output is correct
14 Correct 14 ms 33528 KB Output is correct
15 Correct 14 ms 33620 KB Output is correct
16 Correct 14 ms 33616 KB Output is correct
17 Correct 17 ms 33620 KB Output is correct
18 Correct 14 ms 33620 KB Output is correct
19 Correct 14 ms 33512 KB Output is correct
20 Correct 15 ms 33492 KB Output is correct
21 Correct 16 ms 33612 KB Output is correct
22 Correct 14 ms 33604 KB Output is correct
23 Correct 14 ms 33492 KB Output is correct
24 Correct 15 ms 33624 KB Output is correct
25 Correct 14 ms 33616 KB Output is correct
26 Correct 14 ms 33620 KB Output is correct
27 Correct 14 ms 33632 KB Output is correct
28 Correct 15 ms 33628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 33492 KB Output is correct
2 Correct 5 ms 8788 KB Output is correct
3 Correct 5 ms 8788 KB Output is correct
4 Correct 6 ms 8788 KB Output is correct
5 Correct 5 ms 8768 KB Output is correct
6 Correct 5 ms 8916 KB Output is correct
7 Correct 5 ms 8916 KB Output is correct
8 Correct 7 ms 8920 KB Output is correct
9 Correct 5 ms 8916 KB Output is correct
10 Correct 49 ms 16836 KB Output is correct
11 Correct 61 ms 18692 KB Output is correct
12 Correct 59 ms 18564 KB Output is correct
13 Correct 61 ms 19124 KB Output is correct
14 Correct 55 ms 19636 KB Output is correct
15 Correct 14 ms 33620 KB Output is correct
16 Correct 14 ms 33492 KB Output is correct
17 Correct 16 ms 33556 KB Output is correct
18 Correct 14 ms 33596 KB Output is correct
19 Correct 14 ms 33528 KB Output is correct
20 Correct 14 ms 33620 KB Output is correct
21 Correct 14 ms 33616 KB Output is correct
22 Correct 17 ms 33620 KB Output is correct
23 Correct 14 ms 33620 KB Output is correct
24 Correct 14 ms 33512 KB Output is correct
25 Correct 15 ms 33492 KB Output is correct
26 Correct 16 ms 33612 KB Output is correct
27 Correct 14 ms 33604 KB Output is correct
28 Correct 14 ms 33492 KB Output is correct
29 Correct 15 ms 33624 KB Output is correct
30 Correct 14 ms 33616 KB Output is correct
31 Correct 14 ms 33620 KB Output is correct
32 Correct 14 ms 33632 KB Output is correct
33 Correct 15 ms 33628 KB Output is correct
34 Correct 23 ms 35072 KB Output is correct
35 Correct 119 ms 45052 KB Output is correct
36 Correct 97 ms 45104 KB Output is correct
37 Correct 93 ms 45108 KB Output is correct
38 Correct 105 ms 44856 KB Output is correct
39 Correct 122 ms 44884 KB Output is correct
40 Correct 96 ms 43832 KB Output is correct
41 Correct 99 ms 45372 KB Output is correct
42 Correct 100 ms 45048 KB Output is correct
43 Correct 109 ms 50236 KB Output is correct
44 Correct 116 ms 45376 KB Output is correct
45 Correct 132 ms 48368 KB Output is correct
46 Correct 92 ms 44980 KB Output is correct
47 Correct 116 ms 45208 KB Output is correct
48 Correct 125 ms 47404 KB Output is correct
49 Correct 78 ms 41320 KB Output is correct
50 Correct 77 ms 40256 KB Output is correct
51 Correct 105 ms 46052 KB Output is correct
52 Correct 129 ms 48604 KB Output is correct
53 Correct 162 ms 50100 KB Output is correct
54 Correct 148 ms 50352 KB Output is correct
55 Correct 127 ms 49700 KB Output is correct
56 Correct 160 ms 51548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8788 KB Output is correct
2 Correct 5 ms 8788 KB Output is correct
3 Correct 6 ms 8788 KB Output is correct
4 Correct 5 ms 8768 KB Output is correct
5 Correct 5 ms 8916 KB Output is correct
6 Correct 5 ms 8916 KB Output is correct
7 Correct 7 ms 8920 KB Output is correct
8 Correct 5 ms 8916 KB Output is correct
9 Correct 49 ms 16836 KB Output is correct
10 Correct 61 ms 18692 KB Output is correct
11 Correct 59 ms 18564 KB Output is correct
12 Correct 61 ms 19124 KB Output is correct
13 Correct 55 ms 19636 KB Output is correct
14 Correct 71 ms 17212 KB Output is correct
15 Correct 113 ms 22836 KB Output is correct
16 Correct 109 ms 22588 KB Output is correct
17 Correct 119 ms 23204 KB Output is correct
18 Correct 110 ms 23832 KB Output is correct
19 Correct 292 ms 56156 KB Output is correct
20 Correct 307 ms 56996 KB Output is correct
21 Correct 329 ms 56784 KB Output is correct
22 Correct 317 ms 56752 KB Output is correct
23 Correct 289 ms 57060 KB Output is correct
24 Correct 285 ms 56188 KB Output is correct
25 Correct 296 ms 56668 KB Output is correct
26 Correct 292 ms 56172 KB Output is correct
27 Correct 14 ms 33620 KB Output is correct
28 Correct 14 ms 33492 KB Output is correct
29 Correct 16 ms 33556 KB Output is correct
30 Correct 14 ms 33596 KB Output is correct
31 Correct 14 ms 33528 KB Output is correct
32 Correct 14 ms 33620 KB Output is correct
33 Correct 14 ms 33616 KB Output is correct
34 Correct 17 ms 33620 KB Output is correct
35 Correct 14 ms 33620 KB Output is correct
36 Correct 23 ms 35072 KB Output is correct
37 Correct 119 ms 45052 KB Output is correct
38 Correct 97 ms 45104 KB Output is correct
39 Correct 93 ms 45108 KB Output is correct
40 Correct 105 ms 44856 KB Output is correct
41 Correct 122 ms 44884 KB Output is correct
42 Correct 96 ms 43832 KB Output is correct
43 Correct 99 ms 45372 KB Output is correct
44 Correct 100 ms 45048 KB Output is correct
45 Correct 109 ms 50236 KB Output is correct
46 Correct 116 ms 45376 KB Output is correct
47 Correct 31 ms 35452 KB Output is correct
48 Correct 270 ms 49980 KB Output is correct
49 Correct 227 ms 49908 KB Output is correct
50 Correct 246 ms 49944 KB Output is correct
51 Correct 236 ms 49900 KB Output is correct
52 Correct 243 ms 49196 KB Output is correct
53 Correct 191 ms 47196 KB Output is correct
54 Correct 274 ms 50784 KB Output is correct
55 Correct 246 ms 49860 KB Output is correct
56 Correct 310 ms 54060 KB Output is correct
57 Correct 267 ms 50924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 33492 KB Output is correct
2 Correct 5 ms 8788 KB Output is correct
3 Correct 5 ms 8788 KB Output is correct
4 Correct 6 ms 8788 KB Output is correct
5 Correct 5 ms 8768 KB Output is correct
6 Correct 5 ms 8916 KB Output is correct
7 Correct 5 ms 8916 KB Output is correct
8 Correct 7 ms 8920 KB Output is correct
9 Correct 5 ms 8916 KB Output is correct
10 Correct 49 ms 16836 KB Output is correct
11 Correct 61 ms 18692 KB Output is correct
12 Correct 59 ms 18564 KB Output is correct
13 Correct 61 ms 19124 KB Output is correct
14 Correct 55 ms 19636 KB Output is correct
15 Correct 71 ms 17212 KB Output is correct
16 Correct 113 ms 22836 KB Output is correct
17 Correct 109 ms 22588 KB Output is correct
18 Correct 119 ms 23204 KB Output is correct
19 Correct 110 ms 23832 KB Output is correct
20 Correct 292 ms 56156 KB Output is correct
21 Correct 307 ms 56996 KB Output is correct
22 Correct 329 ms 56784 KB Output is correct
23 Correct 317 ms 56752 KB Output is correct
24 Correct 289 ms 57060 KB Output is correct
25 Correct 285 ms 56188 KB Output is correct
26 Correct 296 ms 56668 KB Output is correct
27 Correct 292 ms 56172 KB Output is correct
28 Correct 14 ms 33620 KB Output is correct
29 Correct 14 ms 33492 KB Output is correct
30 Correct 16 ms 33556 KB Output is correct
31 Correct 14 ms 33596 KB Output is correct
32 Correct 14 ms 33528 KB Output is correct
33 Correct 14 ms 33620 KB Output is correct
34 Correct 14 ms 33616 KB Output is correct
35 Correct 17 ms 33620 KB Output is correct
36 Correct 14 ms 33620 KB Output is correct
37 Correct 23 ms 35072 KB Output is correct
38 Correct 119 ms 45052 KB Output is correct
39 Correct 97 ms 45104 KB Output is correct
40 Correct 93 ms 45108 KB Output is correct
41 Correct 105 ms 44856 KB Output is correct
42 Correct 122 ms 44884 KB Output is correct
43 Correct 96 ms 43832 KB Output is correct
44 Correct 99 ms 45372 KB Output is correct
45 Correct 100 ms 45048 KB Output is correct
46 Correct 109 ms 50236 KB Output is correct
47 Correct 116 ms 45376 KB Output is correct
48 Correct 31 ms 35452 KB Output is correct
49 Correct 270 ms 49980 KB Output is correct
50 Correct 227 ms 49908 KB Output is correct
51 Correct 246 ms 49944 KB Output is correct
52 Correct 236 ms 49900 KB Output is correct
53 Correct 243 ms 49196 KB Output is correct
54 Correct 191 ms 47196 KB Output is correct
55 Correct 274 ms 50784 KB Output is correct
56 Correct 246 ms 49860 KB Output is correct
57 Correct 310 ms 54060 KB Output is correct
58 Correct 267 ms 50924 KB Output is correct
59 Correct 99 ms 40388 KB Output is correct
60 Correct 236 ms 50060 KB Output is correct
61 Correct 221 ms 50044 KB Output is correct
62 Correct 259 ms 50896 KB Output is correct
63 Correct 304 ms 54684 KB Output is correct
64 Correct 14 ms 33512 KB Output is correct
65 Correct 15 ms 33492 KB Output is correct
66 Correct 16 ms 33612 KB Output is correct
67 Correct 14 ms 33604 KB Output is correct
68 Correct 14 ms 33492 KB Output is correct
69 Correct 15 ms 33624 KB Output is correct
70 Correct 14 ms 33616 KB Output is correct
71 Correct 14 ms 33620 KB Output is correct
72 Correct 14 ms 33632 KB Output is correct
73 Correct 15 ms 33628 KB Output is correct
74 Correct 132 ms 48368 KB Output is correct
75 Correct 92 ms 44980 KB Output is correct
76 Correct 116 ms 45208 KB Output is correct
77 Correct 125 ms 47404 KB Output is correct
78 Correct 78 ms 41320 KB Output is correct
79 Correct 77 ms 40256 KB Output is correct
80 Correct 105 ms 46052 KB Output is correct
81 Correct 129 ms 48604 KB Output is correct
82 Correct 162 ms 50100 KB Output is correct
83 Correct 148 ms 50352 KB Output is correct
84 Correct 127 ms 49700 KB Output is correct
85 Correct 160 ms 51548 KB Output is correct
86 Correct 74 ms 39928 KB Output is correct
87 Correct 243 ms 49860 KB Output is correct
88 Correct 264 ms 49960 KB Output is correct
89 Correct 285 ms 51584 KB Output is correct
90 Correct 158 ms 45716 KB Output is correct
91 Correct 171 ms 46452 KB Output is correct
92 Correct 255 ms 50864 KB Output is correct
93 Correct 286 ms 53180 KB Output is correct
94 Correct 307 ms 54508 KB Output is correct
95 Correct 292 ms 54108 KB Output is correct
96 Correct 335 ms 54304 KB Output is correct
97 Correct 313 ms 54408 KB Output is correct