75722024-01-09 19:02:48horvathabelCiklikus rácsháló gráfcpp17Wrong answer 38/4079ms3516 KiB
#include <bits/stdc++.h>
using namespace std;
using ll=long long; 
vector<ll> g[201];

ll bfs(ll x){
	vector<bool>seen; 
	seen.resize(201,false);
	ll tav[201];
	queue<ll> q;
	q.push(x);
	seen[x]=true;
	tav[x]=0;
	ll ans=0; 
	while (!q.empty()){
		ll 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() {
	ll n, m;
	cin>>n>>m;
	swap(n,m);
	for (ll i=1; i<=m;i++){
		g[1+(i-1)*n].push_back(i*n);
		g[i*n].push_back(1+(i-1)*n);
	}
	for (ll 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); 
		}
	}
	ll k;
	cin>>k;
	for (ll u=0; u<k;u++){
		ll a, b;
		cin>>a>>b;
		g[a].push_back(b);
		g[b].push_back(a);
		ll ans=0;
		for (ll i=1; i<=n*m;i++) ans=max(ans, bfs(i));
		cout<<ans<<endl;  
	}
}
SubtaskSumTestVerdictTimeMemory
base38/40
1Accepted0/03ms1816 KiB
2Accepted0/079ms2028 KiB
3Wrong answer0/23ms2476 KiB
4Accepted2/23ms2564 KiB
5Accepted2/23ms2584 KiB
6Accepted2/23ms2660 KiB
7Accepted2/26ms2776 KiB
8Accepted2/26ms2772 KiB
9Accepted2/26ms2864 KiB
10Accepted2/24ms2940 KiB
11Accepted2/26ms3076 KiB
12Accepted2/217ms3192 KiB
13Accepted2/237ms3268 KiB
14Accepted2/27ms3352 KiB
15Accepted2/237ms3516 KiB
16Accepted2/26ms3508 KiB
17Accepted2/230ms3388 KiB
18Accepted2/214ms3388 KiB
19Accepted2/23ms3484 KiB
20Accepted2/23ms3488 KiB
21Accepted2/216ms3476 KiB
22Accepted2/278ms3480 KiB