23752023-01-11 21:18:15TuruTamasCiklikus rácsháló gráfcpp11Accepted 40/4024ms5116 KiB
#include <bits/stdc++.h>
using namespace std;


#define InTheNameOfGod cin.tie(0); cout.tie(0); ios::sync_with_stdio(0);
#define nl '\n'

void print(vector<vector<int>> &s);


int N, M, K, maxx = INT_MAX, L = 500;

void arg(vector<vector<int>> &s, int a, int b) {
    // for (size_t k = 0; k < N*M; k++)
    // {
        
        for (size_t i = 0; i < N*M; i++)
        {
            for (size_t j = 0; j < N*M; j++)
            {
                s[i][j] = min(s[i][a] + s[j][b] + 1, s[i][j]);
                s[i][j] = min(s[i][b] + s[j][a] + 1, s[i][j]);
            } // j
        } // i
    // } // k

    int temp = -1, t;
    for(auto &i : s)
    {
        t = *max_element(i.begin(), i.end());
        temp = t > temp ? t : temp;
    }
    maxx = temp;
    // cout << "KESZ AZ ARGS\n";
    // print(s);
}

void fulld(vector<vector<int>> &s) {
    for (size_t k = 0; k < N*M; k++)
    {
        for (size_t i = 0; i < N*M; i++)
        {
            for (size_t j = 0; j < N*M; j++)
            {
                s[i][j] = min(s[i][k] + s[k][j], s[i][j]);
            } // j
        } // i
    } // k

    int temp = -1, t;
    for(auto &i : s)
    {
        t = *max_element(i.begin(), i.end());
        temp = t > temp ? t : temp;
    }
    maxx = temp;
    // cout << "KESZ AZ ARGS\n";
    // print(s);

}

void print(vector<vector<int>> &s)
{
    cout << "-----\n     ";
    for (size_t i = 0; i < M*N; i++)
    {
        cout << setw(3) << i << ' ';
    }
    cout << nl <<nl;
    for (size_t i = 0; i < N*M; i++)
    {
        cout << setw(3) << i << "  ";
        for (size_t k = 0; k < N*M; k++)
        {
            cout << setw(3) << s[i][k] << " ";
        }
        cout << setw(3) << i << "\n";
    }
    cout << "-----\n";
}

int main() {

    InTheNameOfGod

    cin >> N >> M >> K;
    vector<vector<int>> s(N*M, vector<int>(N*M, L));

    // cout << "d0\n";

    for (size_t i = 0; i < N*M; i++)
    {
        int sor = i / M;
        int osz = i % M;
        // cout << i << " -> " << sor << ":" << osz << "\n";
        if (sor != 0) {
            // cout << "d1\n";
            s[i - M][i] = 1;
            s[i][i - M] = 1;
            // cout << "d2\n";
        }
        if (osz != 0) {
            // cout << "d3\n";
            s[i][i - 1] = 1;
            s[i - 1][i] = 1;
            // cout << "d4\n";
        }
        if (sor != N - 1) {
            // cout << "d5\n";
            s[i][i + M] = 1;
            s[i + M][i] = 1;
            // cout << "d6\n";
        }
        if (osz != M - 1) {
            // cout << "d7\n";
            s[i + 1][i] = 1;
            s[i][i + 1] = 1;
            // cout << "d8\n";
        }
        // cout << "d9\n";
        s[i][i] = 0;
    }
    if(N > 1 && M > 1)
    {
        for(int i = 0; i < N; i++)
        {
            s[i*M][i*M+M-1] = 1;
            s[i*M+M-1][i*M] = 1;
        }
        for(int i = 0; i < M; i++)
        {
            s[i][M*(N-1)+i] = 1;
            s[M*(N-1)+i][i] = 1;
        }
    }
    else if(N > 1)
    {
        s[0][N-1] = 1;
        s[N-1][0] = 1;
    }
    else if(M > 1)
    {
        s[0][M-1] = 1;
        s[M-1][0] = 1;
    }
    fulld(s);
    // print(s);
    int a, b;
    for (size_t i = 0; i <K; i++)
    {
        cin >> a >> b;
        a--;
        b--;
        s[a][b] = 1;
        s[b][a] = 1;
        arg(s, a, b);
        // print(s);
        cout << maxx << "\n";
    }
    // print(s);
    
}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/03ms1824 KiB
2Accepted0/024ms2408 KiB
3Accepted2/22ms2228 KiB
4Accepted2/22ms2436 KiB
5Accepted2/22ms2636 KiB
6Accepted2/22ms2716 KiB
7Accepted2/24ms3008 KiB
8Accepted2/24ms3208 KiB
9Accepted2/24ms3160 KiB
10Accepted2/23ms3260 KiB
11Accepted2/24ms3368 KiB
12Accepted2/217ms3904 KiB
13Accepted2/210ms3624 KiB
14Accepted2/24ms3832 KiB
15Accepted2/210ms4188 KiB
16Accepted2/24ms3976 KiB
17Accepted2/28ms4008 KiB
18Accepted2/24ms4236 KiB
19Accepted2/22ms4200 KiB
20Accepted2/22ms4300 KiB
21Accepted2/24ms4676 KiB
22Accepted2/224ms5116 KiB