69742023-12-23 13:00:53MagyarKendeSZLGRácsháló gráfcpp17Elfogadva 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';
    }
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/03ms1828 KiB
2Elfogadva0/0128ms2076 KiB
3Elfogadva2/23ms2240 KiB
4Elfogadva2/23ms2316 KiB
5Elfogadva2/23ms2448 KiB
6Elfogadva2/23ms2664 KiB
7Elfogadva2/28ms2876 KiB
8Elfogadva2/28ms2960 KiB
9Elfogadva2/28ms3180 KiB
10Elfogadva2/24ms3260 KiB
11Elfogadva2/28ms3176 KiB
12Elfogadva2/226ms3184 KiB
13Elfogadva3/359ms3312 KiB
14Elfogadva3/39ms3396 KiB
15Elfogadva3/359ms3392 KiB
16Elfogadva3/38ms3520 KiB
17Elfogadva3/348ms3608 KiB
18Elfogadva3/318ms3692 KiB
19Elfogadva3/33ms3960 KiB
20Elfogadva3/34ms4032 KiB
21Elfogadva3/321ms4036 KiB
22Elfogadva3/3129ms4232 KiB