72942024-01-06 12:44:38anonCiklikus rácsháló gráfcpp17Accepted 40/40395ms3644 KiB
#include <bits/stdc++.h>
#define MOD(a, b) ((((a) % (b)) + (b)) % (b))
#define FastIO ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
using namespace std;
typedef long long ll;
const ll INF = (1LL << 62);
const ll D4[][2] = {{ -1, 0 }, { 0, -1 }, { 0, 1 }, { 1, 0 }};
int main() {
    FastIO;
    ll i, j, u, v, nx, ny, ans, N, M, K;
    cin >> N >> M >> K;
    vector<vector<ll>> graph(N * M + 1);
    while(K--) {
        cin >> u >> v;
        graph[u].push_back(v);
        graph[v].push_back(u);
        ans = -INF;
        for(i = 1; i <= ((N * M) < 180 ? (N * M) : ((N * M) / 2 + 1)); i++) {
            vector<bool> vis(N * M + 1, false);
            queue<array<ll, 2>> q;
            q.push({ i, 0 });
            while(!q.empty()) {
                auto [cv, d] = q.front();
                q.pop();
                if(vis[cv])
                    continue;
                vis[cv] = true;
                ans = max(ans, d);
                for(j = 0; j < 4; j++) {
                    nx = MOD((cv - 1) % M + D4[j][0], M);
                    ny = MOD((cv - 1) / M + D4[j][1], N);
                    u = ny * M + nx + 1;
                    if(!vis[u])
                        q.push({ u, d + 1 });
                }
                for(const auto &x : graph[cv]) {
                    if(!vis[x])
                        q.push({ x, d + 1});
                }
            }
        }
        cout << ans << '\n';
    }
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/03ms1828 KiB
2Accepted0/0395ms2072 KiB
3Accepted2/23ms2236 KiB
4Accepted2/24ms2448 KiB
5Accepted2/26ms2536 KiB
6Accepted2/27ms2752 KiB
7Accepted2/239ms3000 KiB
8Accepted2/239ms3284 KiB
9Accepted2/239ms3292 KiB
10Accepted2/218ms3400 KiB
11Accepted2/239ms3540 KiB
12Accepted2/2101ms3432 KiB
13Accepted2/2347ms3428 KiB
14Accepted2/252ms3428 KiB
15Accepted2/2351ms3528 KiB
16Accepted2/241ms3616 KiB
17Accepted2/2293ms3644 KiB
18Accepted2/2104ms3504 KiB
19Accepted2/26ms3492 KiB
20Accepted2/29ms3492 KiB
21Accepted2/2128ms3620 KiB
22Accepted2/2395ms3504 KiB