72902024-01-06 12:39:01anonCiklikus rácsháló gráfcpp17Wrong answer 28/40257ms4128 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 / 2 + 1) * (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
base28/40
1Accepted0/03ms1976 KiB
2Accepted0/0257ms2216 KiB
3Accepted2/23ms2248 KiB
4Accepted2/23ms2468 KiB
5Accepted2/24ms2564 KiB
6Accepted2/24ms2564 KiB
7Accepted2/216ms2684 KiB
8Wrong answer0/216ms2876 KiB
9Wrong answer0/214ms3088 KiB
10Wrong answer0/28ms3300 KiB
11Accepted2/214ms3376 KiB
12Accepted2/2103ms3384 KiB
13Accepted2/2123ms3516 KiB
14Accepted2/219ms3588 KiB
15Accepted2/2114ms3728 KiB
16Accepted2/216ms3600 KiB
17Accepted2/297ms3728 KiB
18Wrong answer0/241ms3808 KiB
19Accepted2/24ms3808 KiB
20Accepted2/24ms3920 KiB
21Wrong answer0/243ms4036 KiB
22Wrong answer0/2257ms4128 KiB