67092023-12-17 18:13:20111Ciklikus rácsháló gráfcpp17Accepted 40/4057ms4340 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define double long double

#define pii pair<int, int>

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#ifdef CB
	freopen("be2.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);
#endif
	int N, M, K;
	cin >> N >> M >> K;
	vector<vector<int>> g(N * M);
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			g[i * M + j].push_back(i * M + (j + 1) % M);
			g[i * M + j].push_back((i + 1) % N * M + j);
			g[i * M + j].push_back(i * M + (j - 1 + M) % M);
			g[i * M + j].push_back((i - 1 + N) % N * M + j);
		}
	}
	while (K--) {
		int a, b;
		cin >> a >> b;
		a--, b--;
		g[a].push_back(b);
		g[b].push_back(a);
		auto solve = [&](int i) {
			vector<int> v(N * M, -1);
			deque<int> q;
			q.push_back(i);
			v[i] = 0;
			int f;
			while (!q.empty()) {
				int j = f = q.front();
				q.pop_front();
				for (int k : g[j]) {
					if (v[k] == -1) {
						v[k] = v[j] + 1;
						q.push_back(k);
					}
				}
			}
			return v[f];
		};
		int ans = 0;
		for (int i = 0; i < N * M; i++) {
			ans = max(ans, solve(i));
		}
		cout << ans << '\n';
	}
	return 0;
}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/03ms1868 KiB
2Accepted0/057ms2108 KiB
3Accepted2/23ms2360 KiB
4Accepted2/23ms2524 KiB
5Accepted2/23ms2656 KiB
6Accepted2/23ms2668 KiB
7Accepted2/24ms2672 KiB
8Accepted2/24ms2872 KiB
9Accepted2/24ms2960 KiB
10Accepted2/24ms2968 KiB
11Accepted2/24ms2972 KiB
12Accepted2/212ms3316 KiB
13Accepted2/226ms3404 KiB
14Accepted2/26ms3616 KiB
15Accepted2/228ms3596 KiB
16Accepted2/24ms3804 KiB
17Accepted2/224ms3696 KiB
18Accepted2/29ms3900 KiB
19Accepted2/23ms3984 KiB
20Accepted2/23ms4080 KiB
21Accepted2/212ms4220 KiB
22Accepted2/257ms4340 KiB