69742023-12-23 13:00:53MagyarKendeSZLGRácsháló gráfcpp17Accepted 50/50129ms4232 KiB
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <climits>
using namespace std;

#define speed cin.tie(0); ios::sync_with_stdio(0)

struct Route {
public:
    int node;
    int length;
    bool operator()(Route a, Route b) {
        return a.length > b.length;
    }
};

vector<vector<int>> neighborS;
vector<int> distanceS;
int N, M, K, m_size;


int dijkstra(int s) {
    for (int i = 1; i <= m_size; i++) {
        distanceS[i] = INT_MAX;
    }

    priority_queue<Route, vector<Route>, Route> pq;

    pq.push({s, 0});
    distanceS[s] = 0;

    while (!pq.empty()) {
        Route next = pq.top();
        pq.pop();

        if (next.length != distanceS[next.node]) continue;

        for (int neighbor : neighborS[next.node]) {

            int dist = distanceS[next.node] + 1;

            if (dist < distanceS[neighbor]) {

                distanceS[neighbor] = dist;
                pq.push({neighbor, dist});
            }
        }
    }

    return *max_element(distanceS.begin() + 1, distanceS.end());
}

int main() {
    speed;

    cin >> N >> M >> K;
    m_size = N * M;

    neighborS.resize(m_size + 1);

    for (int i = 0, p = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            p++;
            if (i) neighborS[p].push_back(p - M);
            if (i < N - 1) neighborS[p].push_back(p + M);
            if (j) neighborS[p].push_back(p - 1);
            if (j < M - 1) neighborS[p].push_back(p + 1);
        }
    }

    distanceS.resize(m_size + 1);

    while (K--) {
        int U, V;
        cin >> U >> V;

        neighborS[U].push_back(V);
        neighborS[V].push_back(U);

        int curr_max = 0;
        for (int i = 1; i <= m_size; i++) {
            curr_max = max(curr_max, dijkstra(i));
        }

        cout << curr_max << '\n';
    }
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1828 KiB
2Accepted0/0128ms2076 KiB
3Accepted2/23ms2240 KiB
4Accepted2/23ms2316 KiB
5Accepted2/23ms2448 KiB
6Accepted2/23ms2664 KiB
7Accepted2/28ms2876 KiB
8Accepted2/28ms2960 KiB
9Accepted2/28ms3180 KiB
10Accepted2/24ms3260 KiB
11Accepted2/28ms3176 KiB
12Accepted2/226ms3184 KiB
13Accepted3/359ms3312 KiB
14Accepted3/39ms3396 KiB
15Accepted3/359ms3392 KiB
16Accepted3/38ms3520 KiB
17Accepted3/348ms3608 KiB
18Accepted3/318ms3692 KiB
19Accepted3/33ms3960 KiB
20Accepted3/34ms4032 KiB
21Accepted3/321ms4036 KiB
22Accepted3/3129ms4232 KiB