#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define ins insert
#define pb push_back
#define endl '\n'
#define putr(x) cout<<x<<endl;return;
#define all(x) x.begin(),x.end()
const int mod = 1e9 +7, sze = 4005, inf = INT_MAX, LL = 30;
const int dx[4] = {-1,1,0,0};
const int dy[4] = {0,0,-1,1};
int comp[sze][sze];
map<pair<int,int>,int> var;
vector<int> graph[sze*sze];
int used[sze*sze];
void rush(){
int n,m;
cin>>n>>m;
vector<vector<char>> arr(n,vector<char>(m));
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
comp[i][j]=0;
cin>>arr[i][j];
}
}
int x,y;
int c=1,a,b;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(arr[i][j]!='.'){
if(!comp[i][j]){
comp[i][j]= (c++);
}
for(int k=0;k<4;k++){
x= i + dx[k];
y = j + dy[k];
if( x>=0 && y>=0 && x<n && y<m){
if(arr[i][j] == arr[x][y]){
comp[x][y]=comp[i][j];
}
else{
if(arr[x][y]!='.' && comp[x][y]){
a = comp[x][y];
b = comp[i][j];
if(!var[make_pair(min(a,b),max(a,b))]){
var[make_pair(min(a,b),max(a,b))]=1;
graph[a].pb(b);
graph[b].pb(a);
}
}
}
}
}
}
}
}
int ans=1;
queue< pair<int,int>> q;
q.push({1,1});
while(!q.empty()){
pair<int,int> node =q.front();
q.pop();
if(!used[node.first]){
ans=max(ans,node.second);
used[node.first]=1;
for(auto v:graph[node.first]){
if(!used[v]){
q.push({v,node.second+1});
}
}
}
}
putr(ans);
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tt = 1;
// cin>>tt;
while(tt--){
rush();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
159 ms |
392692 KB |
Output isn't correct |
2 |
Correct |
86 ms |
378012 KB |
Output is correct |
3 |
Incorrect |
97 ms |
378184 KB |
Output isn't correct |
4 |
Incorrect |
116 ms |
382160 KB |
Output isn't correct |
5 |
Incorrect |
109 ms |
380756 KB |
Output isn't correct |
6 |
Correct |
115 ms |
377808 KB |
Output is correct |
7 |
Incorrect |
121 ms |
378184 KB |
Output isn't correct |
8 |
Incorrect |
119 ms |
378076 KB |
Output isn't correct |
9 |
Incorrect |
114 ms |
378440 KB |
Output isn't correct |
10 |
Incorrect |
134 ms |
380488 KB |
Output isn't correct |
11 |
Incorrect |
141 ms |
379208 KB |
Output isn't correct |
12 |
Incorrect |
147 ms |
383560 KB |
Output isn't correct |
13 |
Incorrect |
130 ms |
380728 KB |
Output isn't correct |
14 |
Incorrect |
127 ms |
380648 KB |
Output isn't correct |
15 |
Incorrect |
164 ms |
390472 KB |
Output isn't correct |
16 |
Incorrect |
188 ms |
392776 KB |
Output isn't correct |
17 |
Incorrect |
136 ms |
387220 KB |
Output isn't correct |
18 |
Incorrect |
113 ms |
382280 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
120 ms |
394312 KB |
Output isn't correct |
2 |
Incorrect |
277 ms |
426800 KB |
Output isn't correct |
3 |
Incorrect |
1060 ms |
695968 KB |
Output isn't correct |
4 |
Incorrect |
326 ms |
448072 KB |
Output isn't correct |
5 |
Correct |
1708 ms |
929608 KB |
Output is correct |
6 |
Incorrect |
1634 ms |
668212 KB |
Output isn't correct |
7 |
Incorrect |
157 ms |
395080 KB |
Output isn't correct |
8 |
Incorrect |
157 ms |
394316 KB |
Output isn't correct |
9 |
Incorrect |
157 ms |
379220 KB |
Output isn't correct |
10 |
Correct |
151 ms |
378184 KB |
Output is correct |
11 |
Incorrect |
159 ms |
394580 KB |
Output isn't correct |
12 |
Correct |
156 ms |
379720 KB |
Output is correct |
13 |
Incorrect |
313 ms |
426808 KB |
Output isn't correct |
14 |
Incorrect |
232 ms |
406880 KB |
Output isn't correct |
15 |
Incorrect |
213 ms |
405064 KB |
Output isn't correct |
16 |
Incorrect |
267 ms |
401480 KB |
Output isn't correct |
17 |
Incorrect |
581 ms |
497480 KB |
Output isn't correct |
18 |
Incorrect |
426 ms |
477000 KB |
Output isn't correct |
19 |
Incorrect |
390 ms |
448172 KB |
Output isn't correct |
20 |
Incorrect |
393 ms |
456264 KB |
Output isn't correct |
21 |
Incorrect |
759 ms |
578868 KB |
Output isn't correct |
22 |
Correct |
1686 ms |
926640 KB |
Output is correct |
23 |
Incorrect |
966 ms |
605176 KB |
Output isn't correct |
24 |
Incorrect |
727 ms |
610548 KB |
Output isn't correct |
25 |
Incorrect |
1272 ms |
732500 KB |
Output isn't correct |
26 |
Correct |
449 ms |
498964 KB |
Output is correct |
27 |
Incorrect |
549 ms |
520776 KB |
Output isn't correct |
28 |
Incorrect |
1542 ms |
668168 KB |
Output isn't correct |
29 |
Incorrect |
1378 ms |
616756 KB |
Output isn't correct |
30 |
Incorrect |
1077 ms |
583364 KB |
Output isn't correct |
31 |
Execution timed out |
2082 ms |
821872 KB |
Time limit exceeded |
32 |
Incorrect |
596 ms |
538180 KB |
Output isn't correct |