72912024-01-06 12:39:53anonCiklikus rácsháló gráfcpp17Időlimit túllépés 38/40582ms4124 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 * 3 / 4; 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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base38/40
1Elfogadva0/03ms1828 KiB
2Időlimit túllépés0/0582ms2160 KiB
3Elfogadva2/23ms2396 KiB
4Elfogadva2/24ms2640 KiB
5Elfogadva2/24ms2756 KiB
6Elfogadva2/26ms2924 KiB
7Elfogadva2/229ms3120 KiB
8Elfogadva2/229ms3208 KiB
9Elfogadva2/229ms3404 KiB
10Elfogadva2/214ms3592 KiB
11Elfogadva2/229ms3804 KiB
12Elfogadva2/2149ms3904 KiB
13Elfogadva2/2259ms3980 KiB
14Elfogadva2/239ms3884 KiB
15Elfogadva2/2261ms4124 KiB
16Elfogadva2/232ms3884 KiB
17Elfogadva2/2218ms3896 KiB
18Elfogadva2/279ms3888 KiB
19Elfogadva2/26ms4012 KiB
20Elfogadva2/28ms4112 KiB
21Elfogadva2/294ms4092 KiB
22Időlimit túllépés0/2558ms3544 KiB