Submission #1043818

# Submission time Handle Problem Language Result Execution time Memory
1043818 2024-08-04T19:38:37 Z VMaksimoski008 Swapping Cities (APIO20_swap) C++17
67 / 100
2000 ms 60092 KB
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;

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

int par[maxn+5], cycle[maxn+5], deg[maxn+5], P[maxn+5], pref[maxn+5], good[maxn+5], d[maxn+5], n, m, N, up[maxn+5][LOG];
vector<int> graph[maxn+5];
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) cycle[n] = 1;
    par[n] = par[a] = par[b] = n;
    if(good[a] || good[b]) good[n] = 1;
    if(cycle[a] || cycle[b]) cycle[n] = 1;
    graph[n].push_back(a);
    if(a != b) graph[n].push_back(b);
}

void dfs(int u) {
    for(int i=1; i<LOG; i++) up[u][i] = up[up[u][i-1]][i-1];

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

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;
        cycle[i] = deg[i] = good[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) good[n+1] = 1;
        if(deg[v] >= 3) good[n+1] = 1;
        uni(u, v);
    }

    for(int i=1; i<=n; i++) pref[i] = (good[i] || cycle[i]);
    dfs(n);
}

int getMinimumFuelCapacity(int X, int Y) {
    X++; Y++;
    int lca = get_lca(X, Y);
    if(!pref[lca]) return -1;
    
    while(true) {
        if(!lca) return -1;
        if(good[lca] || cycle[lca]) return edges[lca-N-1][0];
        lca = up[lca][0];
    }

    return -1;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16984 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 3 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 16984 KB Output is correct
9 Correct 46 ms 33740 KB Output is correct
10 Correct 65 ms 37828 KB Output is correct
11 Correct 59 ms 37328 KB Output is correct
12 Correct 64 ms 38856 KB Output is correct
13 Correct 59 ms 42696 KB Output is correct
14 Correct 49 ms 33736 KB Output is correct
15 Correct 101 ms 39688 KB Output is correct
16 Correct 103 ms 39156 KB Output is correct
17 Correct 125 ms 40476 KB Output is correct
18 Correct 135 ms 44332 KB Output is correct
19 Correct 48 ms 22800 KB Output is correct
20 Correct 118 ms 45128 KB Output is correct
21 Correct 109 ms 44624 KB Output is correct
22 Correct 122 ms 46384 KB Output is correct
23 Execution timed out 2041 ms 48584 KB Time limit exceeded
24 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16984 KB Output is correct
2 Correct 2 ms 16988 KB Output is correct
3 Correct 120 ms 47976 KB Output is correct
4 Correct 112 ms 49300 KB Output is correct
5 Correct 111 ms 48476 KB Output is correct
6 Correct 108 ms 49280 KB Output is correct
7 Correct 117 ms 48964 KB Output is correct
8 Correct 110 ms 47736 KB Output is correct
9 Correct 111 ms 48576 KB Output is correct
10 Correct 106 ms 47360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16984 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 3 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 16984 KB Output is correct
9 Correct 2 ms 16984 KB Output is correct
10 Correct 2 ms 16988 KB Output is correct
11 Correct 2 ms 16988 KB Output is correct
12 Correct 2 ms 16988 KB Output is correct
13 Correct 2 ms 16984 KB Output is correct
14 Correct 2 ms 16988 KB Output is correct
15 Correct 2 ms 17028 KB Output is correct
16 Correct 2 ms 16988 KB Output is correct
17 Correct 2 ms 16984 KB Output is correct
18 Correct 2 ms 17116 KB Output is correct
19 Correct 2 ms 16988 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 17244 KB Output is correct
23 Correct 2 ms 16988 KB Output is correct
24 Correct 2 ms 16988 KB Output is correct
25 Correct 2 ms 16988 KB Output is correct
26 Correct 2 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 16984 KB Output is correct
2 Correct 2 ms 16984 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 16984 KB Output is correct
10 Correct 46 ms 33740 KB Output is correct
11 Correct 65 ms 37828 KB Output is correct
12 Correct 59 ms 37328 KB Output is correct
13 Correct 64 ms 38856 KB Output is correct
14 Correct 59 ms 42696 KB Output is correct
15 Correct 2 ms 16988 KB Output is correct
16 Correct 2 ms 16988 KB Output is correct
17 Correct 2 ms 16988 KB Output is correct
18 Correct 2 ms 16984 KB Output is correct
19 Correct 2 ms 16988 KB Output is correct
20 Correct 2 ms 17028 KB Output is correct
21 Correct 2 ms 16988 KB Output is correct
22 Correct 2 ms 16984 KB Output is correct
23 Correct 2 ms 17116 KB Output is correct
24 Correct 2 ms 16988 KB Output is correct
25 Correct 2 ms 16988 KB Output is correct
26 Correct 3 ms 16988 KB Output is correct
27 Correct 2 ms 17244 KB Output is correct
28 Correct 2 ms 16988 KB Output is correct
29 Correct 2 ms 16988 KB Output is correct
30 Correct 2 ms 16988 KB Output is correct
31 Correct 2 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 9 ms 18712 KB Output is correct
35 Correct 63 ms 38708 KB Output is correct
36 Correct 63 ms 38596 KB Output is correct
37 Correct 60 ms 38600 KB Output is correct
38 Correct 58 ms 38340 KB Output is correct
39 Correct 55 ms 38352 KB Output is correct
40 Correct 59 ms 36464 KB Output is correct
41 Correct 69 ms 38868 KB Output is correct
42 Correct 66 ms 38856 KB Output is correct
43 Correct 60 ms 43976 KB Output is correct
44 Correct 59 ms 38856 KB Output is correct
45 Correct 91 ms 51304 KB Output is correct
46 Correct 56 ms 38852 KB Output is correct
47 Correct 67 ms 38988 KB Output is correct
48 Correct 73 ms 43144 KB Output is correct
49 Correct 66 ms 51388 KB Output is correct
50 Correct 52 ms 43204 KB Output is correct
51 Correct 77 ms 46536 KB Output is correct
52 Correct 90 ms 53700 KB Output is correct
53 Correct 110 ms 55232 KB Output is correct
54 Correct 115 ms 60092 KB Output is correct
55 Correct 57 ms 43564 KB Output is correct
56 Correct 112 ms 58048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16984 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 3 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 16984 KB Output is correct
9 Correct 46 ms 33740 KB Output is correct
10 Correct 65 ms 37828 KB Output is correct
11 Correct 59 ms 37328 KB Output is correct
12 Correct 64 ms 38856 KB Output is correct
13 Correct 59 ms 42696 KB Output is correct
14 Correct 49 ms 33736 KB Output is correct
15 Correct 101 ms 39688 KB Output is correct
16 Correct 103 ms 39156 KB Output is correct
17 Correct 125 ms 40476 KB Output is correct
18 Correct 135 ms 44332 KB Output is correct
19 Correct 120 ms 47976 KB Output is correct
20 Correct 112 ms 49300 KB Output is correct
21 Correct 111 ms 48476 KB Output is correct
22 Correct 108 ms 49280 KB Output is correct
23 Correct 117 ms 48964 KB Output is correct
24 Correct 110 ms 47736 KB Output is correct
25 Correct 111 ms 48576 KB Output is correct
26 Correct 106 ms 47360 KB Output is correct
27 Correct 2 ms 16988 KB Output is correct
28 Correct 2 ms 16988 KB Output is correct
29 Correct 2 ms 16988 KB Output is correct
30 Correct 2 ms 16984 KB Output is correct
31 Correct 2 ms 16988 KB Output is correct
32 Correct 2 ms 17028 KB Output is correct
33 Correct 2 ms 16988 KB Output is correct
34 Correct 2 ms 16984 KB Output is correct
35 Correct 2 ms 17116 KB Output is correct
36 Correct 9 ms 18712 KB Output is correct
37 Correct 63 ms 38708 KB Output is correct
38 Correct 63 ms 38596 KB Output is correct
39 Correct 60 ms 38600 KB Output is correct
40 Correct 58 ms 38340 KB Output is correct
41 Correct 55 ms 38352 KB Output is correct
42 Correct 59 ms 36464 KB Output is correct
43 Correct 69 ms 38868 KB Output is correct
44 Correct 66 ms 38856 KB Output is correct
45 Correct 60 ms 43976 KB Output is correct
46 Correct 59 ms 38856 KB Output is correct
47 Correct 13 ms 19228 KB Output is correct
48 Correct 115 ms 45360 KB Output is correct
49 Correct 112 ms 45356 KB Output is correct
50 Correct 108 ms 45356 KB Output is correct
51 Correct 112 ms 45324 KB Output is correct
52 Correct 105 ms 43896 KB Output is correct
53 Correct 90 ms 38072 KB Output is correct
54 Correct 112 ms 46432 KB Output is correct
55 Correct 120 ms 45356 KB Output is correct
56 Correct 141 ms 49456 KB Output is correct
57 Correct 122 ms 46344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16984 KB Output is correct
2 Correct 2 ms 16984 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 16984 KB Output is correct
10 Correct 46 ms 33740 KB Output is correct
11 Correct 65 ms 37828 KB Output is correct
12 Correct 59 ms 37328 KB Output is correct
13 Correct 64 ms 38856 KB Output is correct
14 Correct 59 ms 42696 KB Output is correct
15 Correct 49 ms 33736 KB Output is correct
16 Correct 101 ms 39688 KB Output is correct
17 Correct 103 ms 39156 KB Output is correct
18 Correct 125 ms 40476 KB Output is correct
19 Correct 135 ms 44332 KB Output is correct
20 Correct 120 ms 47976 KB Output is correct
21 Correct 112 ms 49300 KB Output is correct
22 Correct 111 ms 48476 KB Output is correct
23 Correct 108 ms 49280 KB Output is correct
24 Correct 117 ms 48964 KB Output is correct
25 Correct 110 ms 47736 KB Output is correct
26 Correct 111 ms 48576 KB Output is correct
27 Correct 106 ms 47360 KB Output is correct
28 Correct 2 ms 16988 KB Output is correct
29 Correct 2 ms 16988 KB Output is correct
30 Correct 2 ms 16988 KB Output is correct
31 Correct 2 ms 16984 KB Output is correct
32 Correct 2 ms 16988 KB Output is correct
33 Correct 2 ms 17028 KB Output is correct
34 Correct 2 ms 16988 KB Output is correct
35 Correct 2 ms 16984 KB Output is correct
36 Correct 2 ms 17116 KB Output is correct
37 Correct 9 ms 18712 KB Output is correct
38 Correct 63 ms 38708 KB Output is correct
39 Correct 63 ms 38596 KB Output is correct
40 Correct 60 ms 38600 KB Output is correct
41 Correct 58 ms 38340 KB Output is correct
42 Correct 55 ms 38352 KB Output is correct
43 Correct 59 ms 36464 KB Output is correct
44 Correct 69 ms 38868 KB Output is correct
45 Correct 66 ms 38856 KB Output is correct
46 Correct 60 ms 43976 KB Output is correct
47 Correct 59 ms 38856 KB Output is correct
48 Correct 13 ms 19228 KB Output is correct
49 Correct 115 ms 45360 KB Output is correct
50 Correct 112 ms 45356 KB Output is correct
51 Correct 108 ms 45356 KB Output is correct
52 Correct 112 ms 45324 KB Output is correct
53 Correct 105 ms 43896 KB Output is correct
54 Correct 90 ms 38072 KB Output is correct
55 Correct 112 ms 46432 KB Output is correct
56 Correct 120 ms 45356 KB Output is correct
57 Correct 141 ms 49456 KB Output is correct
58 Correct 122 ms 46344 KB Output is correct
59 Correct 48 ms 22800 KB Output is correct
60 Correct 118 ms 45128 KB Output is correct
61 Correct 109 ms 44624 KB Output is correct
62 Correct 122 ms 46384 KB Output is correct
63 Execution timed out 2041 ms 48584 KB Time limit exceeded
64 Halted 0 ms 0 KB -