75732024-01-09 19:03:58horvathabelCiklikus rácsháló gráfcpp17Wrong answer 38/4079ms3944 KiB
#include <bits/stdc++.h>
using namespace std;
using ll=long long; 
vector<ll> g[202];

ll bfs(ll x){
	vector<bool>seen; 
	seen.resize(202,false);
	ll tav[202];
	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/03ms2000 KiB
2Accepted0/079ms2064 KiB
3Wrong answer0/23ms2264 KiB
4Accepted2/23ms2608 KiB
5Accepted2/23ms2748 KiB
6Accepted2/23ms2792 KiB
7Accepted2/26ms2864 KiB
8Accepted2/26ms2944 KiB
9Accepted2/26ms3148 KiB
10Accepted2/24ms3072 KiB
11Accepted2/26ms3308 KiB
12Accepted2/217ms3364 KiB
13Accepted2/237ms3512 KiB
14Accepted2/27ms3484 KiB
15Accepted2/237ms3484 KiB
16Accepted2/26ms3612 KiB
17Accepted2/232ms3652 KiB
18Accepted2/213ms3640 KiB
19Accepted2/23ms3644 KiB
20Accepted2/23ms3792 KiB
21Accepted2/216ms3944 KiB
22Accepted2/278ms3924 KiB