72062024-01-03 14:22:08horvathabelCiklikus rácsháló gráfcpp17Wrong answer 38/4081ms4732 KiB
#include <bits/stdc++.h>
using namespace std;
vector<int> g[201];
int bfs(int x){
	vector<bool>seen; 
	seen.resize(201,false);
	int tav[201];
	queue<int> q;
	q.push(x);
	seen[x]=true;
	tav[x]=0;
	int ans=0; 
	while (!q.empty()){
		int v=q.front();

		q.pop();
		for (auto edge:g[v]){
			if (!seen[edge]){
				tav[edge]=tav[v]+1;
				ans=max(ans, tav[edge]);
				q.push(edge);
				seen[edge]=true; 
			}
		}
	}
	return ans; 


}
int main() {
	int n, m;
	cin>>n>>m;
	swap(n,m);
	for (int i=1; i<=m;i++){
		g[1+(i-1)*n].push_back(i*n);
		g[i*n].push_back(1+(i-1)*n);
	}
	for (int i=1; i<=n*m;i++){
			if ((i)%n!=1){
				g[i].push_back(i-1);
			}
			if ((i)%n!=0) g[i].push_back(i+1);
			if (i<(m-1)*n+1){
				g[i].push_back(i+n);
				g[i+n].push_back(i);
			}
			if (i<=n){
				g[i].push_back(i+(m-1)*n); 
				g[i+(m-1)*n].push_back(i); 
		}
	}
	int k;
	cin>>k;
	for (int u=0; u<k;u++){
		int a, b;
		cin>>a>>b;
		g[a].push_back(b);
		g[b].push_back(a);
		int ans=0;
		for (int i=1; i<=n*m;i++) ans=max(ans, bfs(i));
		cout<<ans<<endl;  
	}
}
SubtaskSumTestVerdictTimeMemory
base38/40
1Accepted0/03ms1816 KiB
2Accepted0/081ms2196 KiB
3Wrong answer0/23ms2236 KiB
4Accepted2/23ms2372 KiB
5Accepted2/23ms2588 KiB
6Accepted2/23ms2924 KiB
7Accepted2/26ms2928 KiB
8Accepted2/26ms2940 KiB
9Accepted2/26ms2940 KiB
10Accepted2/24ms3136 KiB
11Accepted2/26ms3352 KiB
12Accepted2/217ms3576 KiB
13Accepted2/237ms3664 KiB
14Accepted2/28ms3792 KiB
15Accepted2/237ms3936 KiB
16Accepted2/26ms4156 KiB
17Accepted2/232ms4244 KiB
18Accepted2/214ms4732 KiB
19Accepted2/23ms4576 KiB
20Accepted2/24ms4480 KiB
21Accepted2/216ms4548 KiB
22Accepted2/281ms4564 KiB