7812022-01-11 11:34:03kidesoCiklikus rácsháló gráfcpp11Accepted 40/4064ms2228 KiB
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

int N, M, K;

vector<vector<int> > x;

bool szomszed(int a, int b)
{
	if (1 <= a && a <= M && b % M == a % M && (b - a) / M == N - 1) return true;
	if (a % M == 1 && b - a == M - 1) return true;
	if (abs(a - b) == M) return true;
	if (a % M != 0 && b - a == 1) return true;

	return false;
}

void szel(int k, vector<vector<int> >& y)
{
	vector <bool> l(N * M + 1, false);
	queue <int> sor;

	l[k] = true;
	sor.push(k);
	y[k][k] = 0;

	while (!sor.empty())
	{
		int csp = sor.front();
		sor.pop();

		for (auto e : x[csp])
			if (!l[e]) {
				l[e] = true;
				y[k][e] = y[k][csp] + 1;
				sor.push(e);
		}
	
	}

}

void old()
{
	int maxi = -1;
	vector<vector<int> > y(N * M + 1, vector<int>(N * M + 1, 0));

	for (int i = 1; i <= N * M; ++i)
		szel(i,y);
	
	for (auto e : y)
		for (auto f : e)
			maxi = max(maxi, f);

	cout << maxi << '\n';
}

int main()
{
	cin >> N >> M >> K;

	x.resize(N*M + 1);
	
	for (int i = 1; i <= N * M; ++i)
		for (int j = i + 1; j <= N * M; ++j)
			if (szomszed(i, j)) {
				x[i].push_back(j);
				x[j].push_back(i);
			}



	while (K)
	{
		int a, b;
		cin >> a >> b;
		x[a].push_back(b);
		x[b].push_back(a);

		old();
		--K;
	}

	return 0;
}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/02ms1740 KiB
2Accepted0/064ms2084 KiB
3Accepted2/22ms1844 KiB
4Accepted2/22ms1848 KiB
5Accepted2/21ms1860 KiB
6Accepted2/21ms1860 KiB
7Accepted2/24ms1912 KiB
8Accepted2/24ms1920 KiB
9Accepted2/24ms1920 KiB
10Accepted2/22ms1900 KiB
11Accepted2/24ms1928 KiB
12Accepted2/214ms2184 KiB
13Accepted2/228ms1992 KiB
14Accepted2/27ms1940 KiB
15Accepted2/239ms1996 KiB
16Accepted2/26ms1944 KiB
17Accepted2/226ms1992 KiB
18Accepted2/210ms1940 KiB
19Accepted2/21ms1924 KiB
20Accepted2/22ms1932 KiB
21Accepted2/213ms1960 KiB
22Accepted2/264ms2228 KiB