Submission #979567

# Submission time Handle Problem Language Result Execution time Memory
979567 2024-05-11T07:51:29 Z vjudge1 Swapping Cities (APIO20_swap) C++17
0 / 100
2000 ms 20792 KB
#include "swap.h"
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 100100;
const int INF = 1e9 + 10;

struct DSU {
    vector<int> p, r, c;
    void init(int n, vector<int> c) {
        p.resize(n, 0);
        r.resize(n, 1);
        this->c = c;
        iota(p.begin(), p.end(), 0);
    }
    int find(int x) {
        return p[x] = (p[x] == x ? x : find(p[x]));
    }
    void unite(int u, int v) {
        u = find(u);
        v = find(v);
        if (u == v) return;
        if (r[u] < r[v]) swap(u, v);
        p[v] = u;
        r[u] += r[v];
        c[u] += c[v];
    }
};

struct edge {
    int u, v, w;
};

int n, m;
vector<pair<int, int>> G[N];
vector<edge> edges;

void init(int N, int M,
          vector<int> U, vector<int> V, vector<int> W) {
    n = N, m = M;
    edges.resize(m);
    
    for (int i = 0; i < m; i++) {
        edges[i] = {U[i], V[i], W[i]};
        G[U[i]].push_back({V[i], W[i]});
        G[V[i]].push_back({U[i], W[i]});
    }
    
    sort(edges.begin(), edges.end(), [](edge x, edge y) {
        return (x.w < y.w);
    });
}

vector<int> g[N];

vector<bool> vis;
vector<int> par;

void travel(int s) {
    vis[s] = true;
    for (int to : g[s]) if (!vis[to]) {
        par[to] = s;
        travel(to);
    }
}

vector<int> find_path(int x, int y) {
    vis.assign(n, 0);
    par.assign(n, 0);
    travel(x);
    vector<int> res;
    while (y != x) {
        res.push_back(y);
        y = par[y];
    }
    res.push_back(x);
    reverse(res.begin(), res.end());
    return res;
}

int getMinimumFuelCapacity(int X, int Y) {
    for (int i = 0; i < n; i++) 
        g[i].clear();
    
    int res = -INF, mn = INF;
    DSU ds;
    vector<int> c(n, 0);
    ds.init(n, c);

    for (auto [u, v, w] : edges) {
        ds.unite(u, v);
        g[u].push_back(v);
        g[v].push_back(u);
        if (ds.find(X) == ds.find(Y)) {
            res = w;
            break;
        }
    }

    vector<int> vertices = find_path(X, Y);

    for (int u : vertices) 
        c[u] = 1;
    
    {
        for (int i = 1; i < (int)vertices.size() - 1; i++) {
            for (auto [to, w] : G[vertices[i]]) if (to != vertices[i + 1] && to != vertices[i - 1]) {
                mn = min(mn, w);
            }
        }
        for (auto [to, w] : G[X]) {
            if (c[to] && vertices[1] != to) 
                mn = min(mn, w);
        }
        for (auto [to, w] : G[Y]) {
            if (c[to] && vertices[(int)vertices.size() - 2] != to) 
                mn = min(mn, w);
        }
    }

    ds.init(n, c);
    for (auto [u, v, w] : edges) {
        if (c[u] & c[v]) continue;
        ds.unite(u, v);
        if (ds.c[ds.find(u)] > 1) {
            mn = min(mn, w);
            break;
        }
    }

    if (mn == INF) return -1;
    return max(mn, res); 
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 5208 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5464 KB Output is correct
9 Correct 61 ms 17644 KB Output is correct
10 Correct 84 ms 19732 KB Output is correct
11 Correct 117 ms 20148 KB Output is correct
12 Correct 128 ms 20340 KB Output is correct
13 Correct 92 ms 20792 KB Output is correct
14 Execution timed out 2068 ms 18132 KB Time limit exceeded
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Execution timed out 2080 ms 18828 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 5208 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5464 KB Output is correct
9 Correct 2 ms 4952 KB Output is correct
10 Correct 2 ms 5212 KB Output is correct
11 Correct 2 ms 5212 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Incorrect 3 ms 5208 KB Output isn't correct
16 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 1 ms 4956 KB Output is correct
6 Correct 2 ms 5212 KB Output is correct
7 Correct 2 ms 5208 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 2 ms 5464 KB Output is correct
10 Correct 61 ms 17644 KB Output is correct
11 Correct 84 ms 19732 KB Output is correct
12 Correct 117 ms 20148 KB Output is correct
13 Correct 128 ms 20340 KB Output is correct
14 Correct 92 ms 20792 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
18 Correct 2 ms 5212 KB Output is correct
19 Correct 2 ms 5212 KB Output is correct
20 Incorrect 3 ms 5208 KB Output isn't correct
21 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 5208 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5464 KB Output is correct
9 Correct 61 ms 17644 KB Output is correct
10 Correct 84 ms 19732 KB Output is correct
11 Correct 117 ms 20148 KB Output is correct
12 Correct 128 ms 20340 KB Output is correct
13 Correct 92 ms 20792 KB Output is correct
14 Execution timed out 2068 ms 18132 KB Time limit exceeded
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 1 ms 4956 KB Output is correct
6 Correct 2 ms 5212 KB Output is correct
7 Correct 2 ms 5208 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 2 ms 5464 KB Output is correct
10 Correct 61 ms 17644 KB Output is correct
11 Correct 84 ms 19732 KB Output is correct
12 Correct 117 ms 20148 KB Output is correct
13 Correct 128 ms 20340 KB Output is correct
14 Correct 92 ms 20792 KB Output is correct
15 Execution timed out 2068 ms 18132 KB Time limit exceeded
16 Halted 0 ms 0 KB -