214182026-01-13 08:00:11hunzombiRácsháló gráfcpp11Wrong answer 7/502ms508 KiB
#include <bits/stdc++.h>
using namespace std;

int calcDist(vector<vector<int>>& adj, int start) {
    vector<int> dist(adj.size(), 1e9);
    dist[start] = 0;
    queue<int> q;
    q.push(start);
    int ans = -1;
    while (!q.empty()) {
        int node = q.front();
        q.pop();

        for (int next : adj[node]) {
            if (dist[next] == 1e9) {
                dist[next] = dist[node] + 1;
                ans = max(ans, dist[next]);
                q.push(next);
            }
        }
    }

    return ans;
}

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

    int n, m, k;
    cin >> n >> m >> k;

    int last = n * m;

    vector<vector<int>> adj(last);

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            int idx = i * m + j;
            if (j < m - 1) {
                adj[idx].push_back(idx + 1);
                adj[idx + 1].push_back(idx);
            }
            if (i < n - 1) {
                adj[idx].push_back(idx + m);
                adj[idx + m].push_back(idx);
            }
        }
    }

    for (int i=0; i < k; i++) {
        int u, v;
        cin >> u >> v;
        u--; v--;
        adj[u].push_back(v);
        adj[v].push_back(u);

        cout << max(calcDist(adj, 0), calcDist(adj, m - 1)) << '\n';
    }

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base7/50
1Accepted0/01ms316 KiB
2Wrong answer0/01ms316 KiB
3Accepted2/21ms316 KiB
4Accepted2/21ms316 KiB
5Wrong answer0/21ms332 KiB
6Wrong answer0/21ms316 KiB
7Wrong answer0/21ms316 KiB
8Wrong answer0/21ms316 KiB
9Wrong answer0/21ms316 KiB
10Wrong answer0/21ms424 KiB
11Wrong answer0/21ms316 KiB
12Wrong answer0/21ms316 KiB
13Wrong answer0/31ms316 KiB
14Wrong answer0/31ms508 KiB
15Wrong answer0/31ms316 KiB
16Wrong answer0/31ms316 KiB
17Accepted3/32ms316 KiB
18Wrong answer0/31ms316 KiB
19Wrong answer0/31ms316 KiB
20Wrong answer0/31ms316 KiB
21Wrong answer0/31ms316 KiB
22Wrong answer0/31ms316 KiB