72422024-01-05 10:22:56Error42Hírvivők csoportosítása (40)cpp17Accepted 40/4024ms6940 KiB
#include <algorithm>
#include <iostream>
#include <vector>
#include <tuple>

using namespace std;

struct person {
    int x, y;
};

vector<int> parent;
vector<int> sz;
int components;

int find(int const x) {
    if (parent[x] == -1)
        return x;

    return parent[x] = find(parent[x]);
}

void merge(int const x, int const y) {
    int px = find(x);
    int py = find(y);

    if (px == py)
        return;

    components--;

    if (sz[px] < sz[py])
        swap(px, py);

    parent[py] = px;
    sz[px] += sz[py];
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m, h, k;
    cin >> n >> m >> h >> k;

    vector<person> people(h);
    for (person& p : people)
        cin >> p.x >> p.y;

    vector<pair<int, pair<int, int>>> distances;

    for (int i = 0; i < h - 1; i++) {
        for (int j = i + 1; j < h; j++) {
            distances.push_back({ abs(people[i].x - people[j].x) + abs(people[i].y - people[j].y), { i, j } });
        }
    }

    sort(distances.begin(), distances.end());

    parent.assign(h, -1);
    sz.assign(h, 1);
    components = h;

    if (components <= k) {
        cout << "0\n";
        return 0;
    }

    for (auto dist : distances) {
        int const d = dist.first;
        int const i = dist.second.first;
        int const j = dist.second.second;

        merge(i, j);

        if (components <= k) {
            cout << d << "\n";
            return 0;
        }
    }
}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/03ms1824 KiB
2Accepted0/019ms5348 KiB
3Accepted1/13ms2364 KiB
4Accepted1/13ms2292 KiB
5Accepted1/16ms3416 KiB
6Accepted1/14ms3512 KiB
7Accepted1/13ms3104 KiB
8Accepted1/13ms3092 KiB
9Accepted1/13ms3108 KiB
10Accepted1/16ms4020 KiB
11Accepted1/14ms3976 KiB
12Accepted1/14ms3976 KiB
13Accepted3/324ms6260 KiB
14Accepted3/324ms6276 KiB
15Accepted3/324ms6276 KiB
16Accepted3/324ms6564 KiB
17Accepted3/324ms6608 KiB
18Accepted3/324ms6712 KiB
19Accepted3/324ms6616 KiB
20Accepted3/324ms6900 KiB
21Accepted3/324ms6940 KiB
22Accepted3/324ms6896 KiB