Submission #979613

# Submission time Handle Problem Language Result Execution time Memory
979613 2024-05-11T08:30:50 Z vjudge1 Swapping Cities (APIO20_swap) C++17
0 / 100
2000 ms 26880 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;
    void init(int n) {
        p.assign(n, 0);
        r.assign(n, 1);
        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];
    }
};

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);
    assert(vis[y]);
    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;
    ds.init(n);

    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;
        }
    }

    set<pair<int, int>> deleted;

    vector<int> vertices = find_path(X, Y);
    for (int i = 0; i < (int)vertices.size() - 1; i++) {
        deleted.insert({vertices[i], vertices[i + 1]});
        deleted.insert({vertices[i + 1], vertices[i]});
    } 
    
    {
        vector<int> a;
        for (auto [to, w] : G[X]) {
            if (deleted.count({X, to})) continue;
            a.push_back(w);
        }
        sort(a.begin(), a.end());
        if ((int)a.size() > 1) 
            mn = min(mn, a[1]);

        a.clear();
        for (auto [to, w] : G[Y]) {
            if (deleted.count({Y, to})) continue;
            a.push_back(w);
        }
        sort(a.begin(), a.end());
        if ((int)a.size() > 1) 
            mn = min(mn, a[1]);

        for (int u : vertices) {
            if (u == X || u == Y) continue;
            for (auto [to, w] : G[u]) 
                if (!deleted.count({u, to})) {
                    mn = min(mn, w);
                }
        }
    }


    ds.init(n);

    for (auto [u, v, w] : edges) {
        if (deleted.count({u, v})) continue;
        
        ds.unite(u, v);
        if (ds.find(X) == ds.find(Y)) {
            mn = min(mn, w);
            break;
        }
    }

    if (mn == INF) return -1;
    return max(mn, res); 
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4956 KB Output is correct
2 Correct 1 ms 5048 KB Output is correct
3 Correct 2 ms 4952 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 5212 KB Output is correct
7 Correct 3 ms 5208 KB Output is correct
8 Correct 3 ms 5212 KB Output is correct
9 Correct 127 ms 19736 KB Output is correct
10 Correct 193 ms 21464 KB Output is correct
11 Correct 298 ms 26252 KB Output is correct
12 Correct 352 ms 26880 KB Output is correct
13 Correct 277 ms 26796 KB Output is correct
14 Execution timed out 2103 ms 23600 KB Time limit exceeded
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4956 KB Output is correct
2 Correct 1 ms 5048 KB Output is correct
3 Execution timed out 2017 ms 17744 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4956 KB Output is correct
2 Correct 1 ms 5048 KB Output is correct
3 Correct 2 ms 4952 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 5212 KB Output is correct
7 Correct 3 ms 5208 KB Output is correct
8 Correct 3 ms 5212 KB Output is correct
9 Correct 1 ms 4952 KB Output is correct
10 Correct 2 ms 5212 KB Output is correct
11 Correct 3 ms 5212 KB Output is correct
12 Correct 3 ms 5208 KB Output is correct
13 Correct 2 ms 5208 KB Output is correct
14 Correct 2 ms 5208 KB Output is correct
15 Incorrect 2 ms 5212 KB Output isn't correct
16 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Correct 3 ms 4956 KB Output is correct
3 Correct 1 ms 5048 KB Output is correct
4 Correct 2 ms 4952 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 3 ms 5208 KB Output is correct
9 Correct 3 ms 5212 KB Output is correct
10 Correct 127 ms 19736 KB Output is correct
11 Correct 193 ms 21464 KB Output is correct
12 Correct 298 ms 26252 KB Output is correct
13 Correct 352 ms 26880 KB Output is correct
14 Correct 277 ms 26796 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 3 ms 5212 KB Output is correct
17 Correct 3 ms 5208 KB Output is correct
18 Correct 2 ms 5208 KB Output is correct
19 Correct 2 ms 5208 KB Output is correct
20 Incorrect 2 ms 5212 KB Output isn't correct
21 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4956 KB Output is correct
2 Correct 1 ms 5048 KB Output is correct
3 Correct 2 ms 4952 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 5212 KB Output is correct
7 Correct 3 ms 5208 KB Output is correct
8 Correct 3 ms 5212 KB Output is correct
9 Correct 127 ms 19736 KB Output is correct
10 Correct 193 ms 21464 KB Output is correct
11 Correct 298 ms 26252 KB Output is correct
12 Correct 352 ms 26880 KB Output is correct
13 Correct 277 ms 26796 KB Output is correct
14 Execution timed out 2103 ms 23600 KB Time limit exceeded
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Correct 3 ms 4956 KB Output is correct
3 Correct 1 ms 5048 KB Output is correct
4 Correct 2 ms 4952 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 3 ms 5208 KB Output is correct
9 Correct 3 ms 5212 KB Output is correct
10 Correct 127 ms 19736 KB Output is correct
11 Correct 193 ms 21464 KB Output is correct
12 Correct 298 ms 26252 KB Output is correct
13 Correct 352 ms 26880 KB Output is correct
14 Correct 277 ms 26796 KB Output is correct
15 Execution timed out 2103 ms 23600 KB Time limit exceeded
16 Halted 0 ms 0 KB -