16092022-11-29 05:58:10kovacs.peter.18fRácsháló gráfcpp11Accepted 50/5059ms4412 KiB
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

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

    int N, M, K;
    cin >> N >> M >> K;
    vector<vector<int>> neighbourS(N * M);
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            if (i - 1 >= 0) {
                neighbourS[i * M + j].push_back((i - 1) * M + j);
            }
            if (i + 1 < N) {
                neighbourS[i * M + j].push_back((i + 1) * M + j);
            }
            if (j - 1 >= 0) {
                neighbourS[i * M + j].push_back(i * M + j - 1);
            }
            if (j + 1 < M) {
                neighbourS[i * M + j].push_back(i * M + j + 1);
            }
        }
    }
    while (K--) {
        int A, B;
        cin >> A >> B;
        neighbourS[A - 1].push_back(B - 1);
        neighbourS[B - 1].push_back(A - 1);
        int maximum = 0;
        for (int i = 0; i < N * M; i++) {
            queue<int> currentS;
            currentS.push(i);
            vector<int> distanceS(N * M, -1);
            distanceS[i] = 0;
            while (!currentS.empty()) {
                int c = currentS.front();
                currentS.pop();
                maximum = max(maximum, distanceS[c]);
                for (auto e : neighbourS[c]) {
                    if (distanceS[e] == -1) {
                        distanceS[e] = distanceS[c] + 1;
                        currentS.push(e);
                    }
                }
            }
        }
        cout << maximum << '\n';
    }
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1700 KiB
2Accepted0/057ms1916 KiB
3Accepted2/22ms2372 KiB
4Accepted2/22ms2324 KiB
5Accepted2/22ms2544 KiB
6Accepted2/23ms2748 KiB
7Accepted2/24ms2992 KiB
8Accepted2/24ms3196 KiB
9Accepted2/24ms3212 KiB
10Accepted2/23ms3352 KiB
11Accepted2/24ms3460 KiB
12Accepted2/29ms3688 KiB
13Accepted3/327ms3796 KiB
14Accepted3/34ms3952 KiB
15Accepted3/328ms4140 KiB
16Accepted3/34ms4336 KiB
17Accepted3/324ms4344 KiB
18Accepted3/39ms4408 KiB
19Accepted3/32ms4408 KiB
20Accepted3/33ms4404 KiB
21Accepted3/312ms4408 KiB
22Accepted3/359ms4412 KiB