This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
*
* ^v^
*
*/
#include <iostream>
#include <string>
#include <cmath>
#include <vector>
#include <iomanip>
#include <map>
#include <numeric>
#include <functional>
#include <algorithm>
#include <set>
#include <queue>
#include <climits>
#include <cstdlib>
#include <chrono>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
// #define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define iamtefu ios_base::sync_with_stdio(false); cin.tie(0);
#define ll int
#define ld long double
#define sc(a) scanf("%lld", &a);
#define scv(a, n) vector<ll> a(n);fl(i,0,n){sc(a[i])}
#define prl(a) fl(i,0,a.size()){printf("%lld ", a[i]);}printf("\n");
#define fl(i,a,n) for (ll i(a); i<n; i++)
#define rfl(i,a,n) for (ll i(n-1); i>=a; i--)
#define print(a) for (auto x:a){cout<<x<<" ";} cout<<"\n";
#define tt int tt; cin>>tt; for(;tt--;)
ll gcd(ll a, ll b){
if (b==0){
return a;
}
return gcd(b, a%b);
}
ll pw(ll a, ll b, ll m){
ll res=1;
a%=m;
while (b){
if (b&1){
res=(res*a)%m;
}
a=(a*a)%m;
b/=2;
}
return res;
}
void scn(){
ll n, m; cin>>n>>m;
vector <string> a(n+1);
fl(i,0,n){
cin>>a[i];
}
vector <ll> di={1, 1, 0, -1, -1, -1, 0, 1};
vector <ll> dj={0, 1, 1, 1, 0, -1, -1, -1};
vector <vector <ll>> vis(n, vector <ll>(m));
ll cur = 0;
function <void(ll, ll, char)> dfs=[&](ll i, ll j, char c){
if (i<0 || i>=n || j<0 || j>=m || vis[i][j] || c!=a[i][j]){
return;
}
vis[i][j]=cur;
for (int d=0; d<8; d+=2){
dfs(i+di[d], j+dj[d], c);
}
};
fl(i,0,n){
fl(j,0,m){
if (a[i][j]!='.' && !vis[i][j]){
cur++;
dfs(i, j, a[i][j]);
}
}
}
vector <set <ll>> ed(cur+2);
fl(i,0,n){
fl(j,0,n){
if (vis[i][j]==0){
continue;
}
for (int d=0; d<8; d+=2){
ll i1 = di[d]+i, j1 = dj[d]+j;
if (i1>=0 && i1<n && j1>=0 && j1<m && vis[i1][j1]!=vis[i][j] && vis[i1][j1]!=0){
ed[vis[i1][j1]].insert(vis[i][j]);
ed[vis[i][j]].insert(vis[i1][j1]);
}
}
}
}
vector <ll> ds(cur+2);
queue <int> q;
ds[1] = 0;
q.push(1);
// for (auto x:vis){
// print(x)
// }
ll ans = 0;
vector <ll> vs(cur+2);
while (!q.empty()){
auto j = q.front();
q.pop();
for (auto x:ed[j]){
if (!vs[x]){
ds[x] = ds[j]+1;
vs[j]++;
q.push(x);
}
}
}
ans = *max_element(ds.begin(), ds.end());
cout<<ans+1<<'\n';
// not necessarily distinct
}
int main(){
iamtefu;
#if defined(airths)
auto t1=chrono::high_resolution_clock::now();
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
//
#endif
// tt
{
scn();
}
#if defined(airths)
auto t2=chrono::high_resolution_clock::now();
ld ti=chrono::duration_cast<chrono::nanoseconds>(t2-t1).count();
ti*=1e-6;
cerr<<"Time: "<<setprecision(12)<<ti;
cerr<<"ms\n";
#endif
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |