Submission #1043813

# Submission time Handle Problem Language Result Execution time Memory
1043813 2024-08-04T19:33:21 Z VMaksimoski008 Swapping Cities (APIO20_swap) C++17
7 / 100
242 ms 53288 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] + (good[v] || cycle[v]);
        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);
    }

    dfs(n);
}

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

    int l=0, r=d[lca], ans=-1;
    while(l <= r) {
        int mid = (l + r) / 2, curr = lca;
        for(int j=LOG-1; j>=0; j--) if(mid & (1 << j)) curr = up[curr][j];
        if(pref[lca] - pref[P[curr]]) ans = curr, r = mid - 1;
        else l = mid + 1;
    }

    return (ans == -1 ? -1 : edges[ans-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 17024 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 17128 KB Output is correct
8 Correct 2 ms 16988 KB Output is correct
9 Correct 52 ms 35276 KB Output is correct
10 Correct 61 ms 39752 KB Output is correct
11 Correct 57 ms 39368 KB Output is correct
12 Correct 81 ms 40976 KB Output is correct
13 Correct 74 ms 44744 KB Output is correct
14 Correct 53 ms 35716 KB Output is correct
15 Correct 115 ms 44136 KB Output is correct
16 Correct 109 ms 43556 KB Output is correct
17 Correct 112 ms 44848 KB Output is correct
18 Correct 242 ms 48936 KB Output is correct
19 Incorrect 51 ms 23640 KB Output isn't correct
20 Halted 0 ms 0 KB -
# 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 113 ms 51816 KB Output is correct
4 Correct 123 ms 53288 KB Output is correct
5 Correct 112 ms 52496 KB Output is correct
6 Correct 114 ms 53036 KB Output is correct
7 Correct 114 ms 52900 KB Output is correct
8 Correct 109 ms 51568 KB Output is correct
9 Correct 110 ms 52512 KB Output is correct
10 Correct 107 ms 51304 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 17024 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 17128 KB Output is correct
8 Correct 2 ms 16988 KB Output is correct
9 Correct 3 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 3 ms 17080 KB Output is correct
13 Correct 3 ms 16984 KB Output is correct
14 Correct 2 ms 16984 KB Output is correct
15 Incorrect 2 ms 17032 KB Output isn't correct
16 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 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 17024 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 17128 KB Output is correct
9 Correct 2 ms 16988 KB Output is correct
10 Correct 52 ms 35276 KB Output is correct
11 Correct 61 ms 39752 KB Output is correct
12 Correct 57 ms 39368 KB Output is correct
13 Correct 81 ms 40976 KB Output is correct
14 Correct 74 ms 44744 KB Output is correct
15 Correct 2 ms 16988 KB Output is correct
16 Correct 2 ms 16988 KB Output is correct
17 Correct 3 ms 17080 KB Output is correct
18 Correct 3 ms 16984 KB Output is correct
19 Correct 2 ms 16984 KB Output is correct
20 Incorrect 2 ms 17032 KB Output isn't correct
21 Halted 0 ms 0 KB -
# 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 17024 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 17128 KB Output is correct
8 Correct 2 ms 16988 KB Output is correct
9 Correct 52 ms 35276 KB Output is correct
10 Correct 61 ms 39752 KB Output is correct
11 Correct 57 ms 39368 KB Output is correct
12 Correct 81 ms 40976 KB Output is correct
13 Correct 74 ms 44744 KB Output is correct
14 Correct 53 ms 35716 KB Output is correct
15 Correct 115 ms 44136 KB Output is correct
16 Correct 109 ms 43556 KB Output is correct
17 Correct 112 ms 44848 KB Output is correct
18 Correct 242 ms 48936 KB Output is correct
19 Correct 113 ms 51816 KB Output is correct
20 Correct 123 ms 53288 KB Output is correct
21 Correct 112 ms 52496 KB Output is correct
22 Correct 114 ms 53036 KB Output is correct
23 Correct 114 ms 52900 KB Output is correct
24 Correct 109 ms 51568 KB Output is correct
25 Correct 110 ms 52512 KB Output is correct
26 Correct 107 ms 51304 KB Output is correct
27 Correct 2 ms 16988 KB Output is correct
28 Correct 2 ms 16988 KB Output is correct
29 Correct 3 ms 17080 KB Output is correct
30 Correct 3 ms 16984 KB Output is correct
31 Correct 2 ms 16984 KB Output is correct
32 Incorrect 2 ms 17032 KB Output isn't correct
33 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 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 17024 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 17128 KB Output is correct
9 Correct 2 ms 16988 KB Output is correct
10 Correct 52 ms 35276 KB Output is correct
11 Correct 61 ms 39752 KB Output is correct
12 Correct 57 ms 39368 KB Output is correct
13 Correct 81 ms 40976 KB Output is correct
14 Correct 74 ms 44744 KB Output is correct
15 Correct 53 ms 35716 KB Output is correct
16 Correct 115 ms 44136 KB Output is correct
17 Correct 109 ms 43556 KB Output is correct
18 Correct 112 ms 44848 KB Output is correct
19 Correct 242 ms 48936 KB Output is correct
20 Correct 113 ms 51816 KB Output is correct
21 Correct 123 ms 53288 KB Output is correct
22 Correct 112 ms 52496 KB Output is correct
23 Correct 114 ms 53036 KB Output is correct
24 Correct 114 ms 52900 KB Output is correct
25 Correct 109 ms 51568 KB Output is correct
26 Correct 110 ms 52512 KB Output is correct
27 Correct 107 ms 51304 KB Output is correct
28 Correct 2 ms 16988 KB Output is correct
29 Correct 2 ms 16988 KB Output is correct
30 Correct 3 ms 17080 KB Output is correct
31 Correct 3 ms 16984 KB Output is correct
32 Correct 2 ms 16984 KB Output is correct
33 Incorrect 2 ms 17032 KB Output isn't correct
34 Halted 0 ms 0 KB -