Submission #674713

# Submission time Handle Problem Language Result Execution time Memory
674713 2022-12-26T00:38:09 Z Vahe Swapping Cities (APIO20_swap) C++17
Compilation error
0 ms 0 KB
#include "swap.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
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)
{
    int a = get(u), b = get(v);
    if (a == b)
    {
        P[a] = min(P[a], w);
        return;
    }
    if (sizes[a] < sizes[b]) swap(a, b), swap(u, v);
    sizes[a] += sizes[b];
    p[v] = u;
    her[v] = w;
    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[a] = min(P[a], w);
}

int go(int u, int w)
{
    if (p[u] == u || her[u] > w) return u;
    return 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] = INT_MAX;
    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);
    }
    //for (int i = 0; i < n; i++)
    //{
    //    cout << i << ' ' << par[i] << endl;
    //}
}

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

int main() {
    cin >> n >> m;
    u_ = v_ = w = vector<int>(m);
    for (int i = 0; i < m; i++) cin >> u_[i] >> v_[i] >> w[i];
    init(n, m, u_, v_, w);
    int q; cin >> q;
    while (q--)
    {
        int x, y; cin >> x >> y;
        cout << getMinimumFuelCapacity(x, y) << endl;
    }
    return 0;
}

Compilation message

/usr/bin/ld: /tmp/ccisjFns.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/cc8X9Zkw.o:swap.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status