31632023-02-21 11:46:07TuruTamasHírvivők csoportosítása (40)cpp17Accepted 40/4021ms6336 KiB
#include <bits/stdc++.h>
using namespace std;

int N, M, H, K;
vector<bool> been;
vector<pair<int, int>> hirek;

void dfs(int& d, int loc, vector<vector<int>>& graph) {
    been[loc] = true;
    for (size_t i = 0; i < hirek.size(); i++)
    {
        if (i == loc || been[i]) continue;
        if (graph[i][loc] <= d) dfs(d, i, graph);
    }
}

void getGroups(int& csoportok, int n, vector<vector<int>>& graph) {
    csoportok = 0;
    been.assign(H, false);
    for (size_t i = 0; i < H; i++)
    {
        if (been[i]) continue;
        csoportok++;
        dfs(n, i, graph);
    }
}

int main() {
    cin >> N >> M >> H >> K;
    int x, y;
    for (size_t i = 0; i < H; i++)
    {
        cin >> x >> y;
        hirek.push_back({x, y});
    }
    vector<vector<int>> graph (H, vector<int>(H));
    for (size_t i = 0; i < H; i++)
    {
        for (size_t k = 0; k < H; k++)
        {
            graph[i][k] = graph[k][i] = abs(hirek[i].first - hirek[k].first) + abs(hirek[k].second - hirek[i].second);
        }
    }
    
    int csoportok, minn = 0, maxn = N+M, n = log2(N+M);
    int lastn = n;
    while (true) {
        int csoportok;
        getGroups(csoportok, n, graph);
        if (csoportok > K) {
            minn = n;
            n = (n+maxn)/2;
        }
        else if (csoportok <= K) {
            maxn = n;
            n = (n+minn)/2;
        }
        if (n == lastn) break;
        lastn = n;
    }
    if (csoportok <= K) {
        cout << n;
    }
    else {
        while (csoportok > K) {
            n++;
            getGroups(csoportok, n, graph);
        }
        cout << n;
    }
}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/03ms1816 KiB
2Accepted0/017ms3740 KiB
3Accepted1/13ms2536 KiB
4Accepted1/13ms2544 KiB
5Accepted1/14ms3188 KiB
6Accepted1/16ms3392 KiB
7Accepted1/14ms3352 KiB
8Accepted1/14ms3560 KiB
9Accepted1/14ms3888 KiB
10Accepted1/16ms3904 KiB
11Accepted1/16ms4096 KiB
12Accepted1/16ms4064 KiB
13Accepted3/320ms5820 KiB
14Accepted3/319ms5820 KiB
15Accepted3/320ms5824 KiB
16Accepted3/321ms5820 KiB
17Accepted3/321ms5924 KiB
18Accepted3/320ms6072 KiB
19Accepted3/320ms6048 KiB
20Accepted3/320ms6176 KiB
21Accepted3/319ms6244 KiB
22Accepted3/319ms6336 KiB