Submission #674620

# Submission time Handle Problem Language Result Execution time Memory
674620 2022-12-25T12:26:34 Z Vahe Swapping Cities (APIO20_swap) C++17
0 / 100
1 ms 296 KB
#include "swap.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<vector<pair<int, int>>> gp;
int n, m;
vector<int> u, v, w, ver, var, par, P, p, her, sizes;

int get(int u)
{
    if (u == par[u]) return u;
    return par[u] = get(par[u]);
}

void union_chains(int u, int v, int w)
{
    if (get(u) == get(v))
    {
        P[get(u)] = min(P[get(u)], w);
        return;
    }
    int a = get(u), b = get(v);
    if (sizes[a] < sizes[b]) swap(a, b), swap(u, v);
    sizes[a] += sizes[b];
    par[b] = par[a];
    her[v] = w, p[v] = u;
    if      (u == ver[a] && v == ver[b]) ver[a] = var[b];
    else if (u == ver[a] && v == var[b]) ver[a] = ver[b];
    else if (u == var[a] && v == ver[b]) var[a] = var[b];
    else if (u == var[a] && v == var[b]) var[a] = ver[b];
    else ver[a] = -1, var[a] = -1, P[u] = min(P[u], w);
}

int go(int u, int w)
{
    if (p[u] == u || her[u] > w) return u;
    go(p[u], w);
}

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
    n = N, m = M;
    vector<pair<int, pair<int, int>>> kox;
    for (int i = 0; i < m; i++) kox.push_back({ W[i], {U[i], V[i]} });
    sort(kox.begin(), kox.end());
    ver = var = par = sizes = P = her = p = vector<int>(n);
    for (int i = 0; i < n; i++) ver[i] = var[i] = par[i] = p[i] = i, sizes[i] = 1, her[i] = P[i] = 2e9;
    for (int i = 0; i < m; i++)
    {
        int u = kox[i].second.first, v = kox[i].second.second;
        union_chains(u, v, kox[i].first);
    }

}

int getMinimumFuelCapacity(int X, int Y) {
    int l = -1, r = 1e9;
    while (l + 1 < r)
    {
        int mij = l + (r - l) / 2;
        int a = go(X, mij), b = go(Y, mij);
        if (a == b && P[a] <= mij) r = mij;
        else l = mij;
    }
    return r ? r : -1;
}

Compilation message

swap.cpp: In function 'int go(int, int)':
swap.cpp:38:7: warning: control reaches end of non-void function [-Wreturn-type]
   38 |     go(p[u], w);
      |     ~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 296 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 296 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 296 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 296 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 296 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 296 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -