#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int label = 1, n, m;
const int mxN = 4e3+10;
const int mxN1 = 2e7+10;
vector<int> adj[mxN1];
bool vis[mxN][mxN];
char matrix[mxN][mxN];
int idx[mxN][mxN], d[mxN1];
vector<int> xy = {1, 0, -1, 0}, yx = {0, 1, 0, -1};
void dfs(int i, int j) {
char c = matrix[i][j];
idx[i][j] = label;
vis[i][j] = true;
for(int k = 0; k < 4; k++) {
int a = i + xy[k], b = j + yx[k];
if(a < 0 || a >= n || b < 0 || b >= m) continue;
if(matrix[a][b] == '.') continue;
if(matrix[a][b] == c) {
if(!vis[a][b]) dfs(a, b);
}
else {
if(vis[a][b]) {
adj[label].push_back(idx[a][b]);
adj[idx[a][b]].push_back(label);
}
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
cin >> matrix[i][j];
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(matrix[i][j] != '.' && !vis[i][j]) {
dfs(i, j);
++label;
}
}
}
queue<int> q;
q.push(1);
d[1] = 1;
int ans = 0;
while(!q.empty()) {
auto node = q.front();
q.pop();
ans = max(ans, d[node]);
for(auto it : adj[node]) {
if(d[it] == 0) {
d[it] = d[node]+1;
q.push(it);
}
}
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
135 ms |
480080 KB |
Output is correct |
2 |
Correct |
107 ms |
470492 KB |
Output is correct |
3 |
Correct |
101 ms |
470868 KB |
Output is correct |
4 |
Correct |
118 ms |
477908 KB |
Output is correct |
5 |
Correct |
107 ms |
474196 KB |
Output is correct |
6 |
Correct |
102 ms |
470496 KB |
Output is correct |
7 |
Correct |
102 ms |
470612 KB |
Output is correct |
8 |
Correct |
103 ms |
471044 KB |
Output is correct |
9 |
Correct |
106 ms |
471340 KB |
Output is correct |
10 |
Correct |
108 ms |
479604 KB |
Output is correct |
11 |
Correct |
105 ms |
479572 KB |
Output is correct |
12 |
Correct |
111 ms |
480796 KB |
Output is correct |
13 |
Correct |
106 ms |
479828 KB |
Output is correct |
14 |
Correct |
106 ms |
480080 KB |
Output is correct |
15 |
Correct |
119 ms |
485620 KB |
Output is correct |
16 |
Correct |
123 ms |
486228 KB |
Output is correct |
17 |
Correct |
117 ms |
483272 KB |
Output is correct |
18 |
Correct |
110 ms |
484576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
119 ms |
517712 KB |
Output is correct |
2 |
Correct |
185 ms |
502396 KB |
Output is correct |
3 |
Correct |
550 ms |
622168 KB |
Output is correct |
4 |
Correct |
208 ms |
528212 KB |
Output is correct |
5 |
Correct |
560 ms |
704156 KB |
Output is correct |
6 |
Correct |
826 ms |
664096 KB |
Output is correct |
7 |
Correct |
115 ms |
564924 KB |
Output is correct |
8 |
Correct |
116 ms |
564296 KB |
Output is correct |
9 |
Correct |
103 ms |
474964 KB |
Output is correct |
10 |
Correct |
104 ms |
474760 KB |
Output is correct |
11 |
Correct |
115 ms |
564784 KB |
Output is correct |
12 |
Correct |
105 ms |
479312 KB |
Output is correct |
13 |
Correct |
177 ms |
513620 KB |
Output is correct |
14 |
Correct |
146 ms |
501840 KB |
Output is correct |
15 |
Correct |
137 ms |
501804 KB |
Output is correct |
16 |
Correct |
137 ms |
489044 KB |
Output is correct |
17 |
Correct |
294 ms |
545620 KB |
Output is correct |
18 |
Correct |
238 ms |
540756 KB |
Output is correct |
19 |
Correct |
204 ms |
528212 KB |
Output is correct |
20 |
Correct |
208 ms |
530044 KB |
Output is correct |
21 |
Correct |
362 ms |
581720 KB |
Output is correct |
22 |
Correct |
555 ms |
704340 KB |
Output is correct |
23 |
Correct |
458 ms |
580956 KB |
Output is correct |
24 |
Correct |
347 ms |
599132 KB |
Output is correct |
25 |
Correct |
859 ms |
645484 KB |
Output is correct |
26 |
Runtime error |
564 ms |
1048576 KB |
Execution killed with signal 9 |
27 |
Runtime error |
587 ms |
1048576 KB |
Execution killed with signal 9 |
28 |
Correct |
833 ms |
665388 KB |
Output is correct |
29 |
Correct |
806 ms |
693908 KB |
Output is correct |
30 |
Correct |
847 ms |
789572 KB |
Output is correct |
31 |
Correct |
973 ms |
682244 KB |
Output is correct |
32 |
Runtime error |
889 ms |
1048576 KB |
Execution killed with signal 9 |