72892024-01-06 12:38:04anonCiklikus rácsháló gráfcpp17Wrong answer 38/40393ms4040 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 / 2; 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
base38/40
1Accepted0/03ms1700 KiB
2Accepted0/0393ms1876 KiB
3Accepted2/23ms2076 KiB
4Accepted2/24ms2416 KiB
5Accepted2/24ms2428 KiB
6Accepted2/24ms2512 KiB
7Accepted2/220ms2724 KiB
8Accepted2/220ms2816 KiB
9Accepted2/220ms3040 KiB
10Wrong answer0/210ms3248 KiB
11Accepted2/220ms3348 KiB
12Accepted2/2101ms3316 KiB
13Accepted2/2175ms3320 KiB
14Accepted2/227ms3536 KiB
15Accepted2/2178ms3536 KiB
16Accepted2/221ms3428 KiB
17Accepted2/2148ms3556 KiB
18Accepted2/254ms3648 KiB
19Accepted2/24ms3832 KiB
20Accepted2/26ms3844 KiB
21Accepted2/264ms3932 KiB
22Accepted2/2393ms4040 KiB