Submission #984413

# Submission time Handle Problem Language Result Execution time Memory
984413 2024-05-16T15:39:42 Z vjudge2 Swapping Cities (APIO20_swap) C++17
17 / 100
2000 ms 58604 KB
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;

const int N = 300005;
const int lg = 19;

int id, deg[N], ans[N], one[N], three[N], p[N], dep[N];
vector<int> adj[N];
vector<vector<int>> lift(N, vector<int> (lg));

int find(int u) {
    return p[u] == u ? u : find(p[u]);
}

void merge(int u, int v) {
    u = find(u), v = find(v);
    if (u == v) return;
    p[v] = u;
    one[u] += one[v];
    three[u] += three[v];
}

void dfs(int u) {
    for (int v : adj[u]) {
        if (!ans[v]) ans[v] = ans[u];
        dep[v] = dep[u] + 1;
        dfs(v);
    }
}

int jump(int s, int d) {
    for (int i = lg - 1; i >= 0; i--) if (d & (1 << i)) s = lift[s][i];
    return s;
}

int lca(int u, int v) {
    if (dep[u] > dep[v]) swap(u, v);
    v = jump(v, dep[v] - dep[u]);
    if (u == v) return u;
    for (int i = lg - 1; i >= 0; i--) {
        int ut = lift[u][i], vt = lift[v][i];
        if (ut != vt) u = ut, v = vt;
    }
    return lift[u][0];
}

void init(int n, int m, vector<int> u, vector<int> v, vector<int> w) {
    vector<array<int, 3>> e;
    for (int i = 0; i < m; i++) e.push_back({w[i], u[i], v[i]});
    sort(e.begin(), e.end());
    for (int i = 0; i < N; i++) p[i] = i;
    id = n - 1;
    for (auto& [w, u, v] : e) {
        if (deg[u] == 1) one[find(u)]--;
        if (deg[v] == 1) one[find(v)]--;
        deg[u]++, deg[v]++;
        if (deg[u] == 1) one[find(u)]++;
        else if (deg[u] == 3) three[find(u)]++;
        if (deg[v] == 1) one[find(v)]++;
        else if (deg[v] == 3) three[find(v)]++;
        u = find(u), v = find(v);
        id++;
        if (u == v) {
            merge(id, u);
            lift[u][0] = id;
            adj[id].push_back(u);
            // cout << id << " --> " << u << '\n';
        } else {
            merge(id, u);
            merge(id, v);
            lift[u][0] = lift[v][0] = id;
            adj[id].push_back(u);
            // cout << id << " --> " << u << '\n';
            adj[id].push_back(v);
            // cout << id << " --> " << v << '\n';
        }
        if (three[id]) ans[id] = w;
        else if (!one[id]) ans[id] = w;
        // cout << "value @ " << id << " = " << w << '\n';
    }
    for (int i = 0; i < N; i++) if (find(i) == i) lift[i][0] = i, dfs(i);
    for (int j = 1; j < lg; j++) for (int i = 0; i < N; i++) {
        lift[i][j] = lift[lift[i][j - 1]][j - 1];
    }
}

int getMinimumFuelCapacity(int X, int Y) {
    if (find(X) != find(Y)) return -1;
    // cout << lca(X, Y) << '\n';
    int l = lca(X, Y);
    if (ans[l] == 0) return -1;
    return ans[l];
}
# Verdict Execution time Memory Grader output
1 Correct 67 ms 47964 KB Output is correct
2 Correct 84 ms 48148 KB Output is correct
3 Correct 73 ms 47960 KB Output is correct
4 Correct 80 ms 47964 KB Output is correct
5 Correct 73 ms 48220 KB Output is correct
6 Correct 74 ms 48292 KB Output is correct
7 Correct 64 ms 48216 KB Output is correct
8 Correct 71 ms 48164 KB Output is correct
9 Correct 138 ms 55256 KB Output is correct
10 Correct 152 ms 56764 KB Output is correct
11 Correct 148 ms 56792 KB Output is correct
12 Correct 169 ms 57320 KB Output is correct
13 Execution timed out 2056 ms 57068 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 67 ms 47964 KB Output is correct
2 Correct 84 ms 48148 KB Output is correct
3 Execution timed out 2073 ms 58604 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 67 ms 47964 KB Output is correct
2 Correct 84 ms 48148 KB Output is correct
3 Correct 73 ms 47960 KB Output is correct
4 Correct 80 ms 47964 KB Output is correct
5 Correct 73 ms 48220 KB Output is correct
6 Correct 74 ms 48292 KB Output is correct
7 Correct 64 ms 48216 KB Output is correct
8 Correct 71 ms 48164 KB Output is correct
9 Correct 71 ms 47960 KB Output is correct
10 Correct 70 ms 48212 KB Output is correct
11 Correct 84 ms 48244 KB Output is correct
12 Correct 68 ms 48220 KB Output is correct
13 Correct 68 ms 48220 KB Output is correct
14 Correct 66 ms 48220 KB Output is correct
15 Correct 77 ms 48220 KB Output is correct
16 Correct 80 ms 48220 KB Output is correct
17 Correct 73 ms 48120 KB Output is correct
18 Correct 71 ms 48220 KB Output is correct
19 Correct 74 ms 48220 KB Output is correct
20 Correct 79 ms 48372 KB Output is correct
21 Correct 73 ms 48216 KB Output is correct
22 Correct 94 ms 48220 KB Output is correct
23 Correct 73 ms 48472 KB Output is correct
24 Correct 83 ms 48212 KB Output is correct
25 Correct 74 ms 48252 KB Output is correct
26 Correct 75 ms 48316 KB Output is correct
27 Correct 71 ms 48220 KB Output is correct
28 Correct 75 ms 48216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 47960 KB Output is correct
2 Correct 67 ms 47964 KB Output is correct
3 Correct 84 ms 48148 KB Output is correct
4 Correct 73 ms 47960 KB Output is correct
5 Correct 80 ms 47964 KB Output is correct
6 Correct 73 ms 48220 KB Output is correct
7 Correct 74 ms 48292 KB Output is correct
8 Correct 64 ms 48216 KB Output is correct
9 Correct 71 ms 48164 KB Output is correct
10 Correct 138 ms 55256 KB Output is correct
11 Correct 152 ms 56764 KB Output is correct
12 Correct 148 ms 56792 KB Output is correct
13 Correct 169 ms 57320 KB Output is correct
14 Execution timed out 2056 ms 57068 KB Time limit exceeded
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 67 ms 47964 KB Output is correct
2 Correct 84 ms 48148 KB Output is correct
3 Correct 73 ms 47960 KB Output is correct
4 Correct 80 ms 47964 KB Output is correct
5 Correct 73 ms 48220 KB Output is correct
6 Correct 74 ms 48292 KB Output is correct
7 Correct 64 ms 48216 KB Output is correct
8 Correct 71 ms 48164 KB Output is correct
9 Correct 138 ms 55256 KB Output is correct
10 Correct 152 ms 56764 KB Output is correct
11 Correct 148 ms 56792 KB Output is correct
12 Correct 169 ms 57320 KB Output is correct
13 Execution timed out 2056 ms 57068 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 71 ms 47960 KB Output is correct
2 Correct 67 ms 47964 KB Output is correct
3 Correct 84 ms 48148 KB Output is correct
4 Correct 73 ms 47960 KB Output is correct
5 Correct 80 ms 47964 KB Output is correct
6 Correct 73 ms 48220 KB Output is correct
7 Correct 74 ms 48292 KB Output is correct
8 Correct 64 ms 48216 KB Output is correct
9 Correct 71 ms 48164 KB Output is correct
10 Correct 138 ms 55256 KB Output is correct
11 Correct 152 ms 56764 KB Output is correct
12 Correct 148 ms 56792 KB Output is correct
13 Correct 169 ms 57320 KB Output is correct
14 Execution timed out 2056 ms 57068 KB Time limit exceeded
15 Halted 0 ms 0 KB -