#include <bits/stdc++.h>
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, m;
std::cin >> n >> m;
std::vector<std::string> g(n);
for (int i = 0; i < n; ++i) {
std::cin >> g[i];
}
std::vector<std::vector<int>> comp(n, std::vector<int>(m, 0));
std::vector<std::vector<int>> adj(n * m + 5);
int num = 0;
std::function<void(int, int, char)> dfs=[&](int x, int y, char c) {
if (x < 0 || x >= n || y < 0 || y >= m || g[x][y] == '.')
return;
if (comp[x][y] && comp[x][y] != num) {
adj[comp[x][y]].push_back(num);
adj[num].push_back(comp[x][y]);
}
if (comp[x][y] || g[x][y] != c)
return;
comp[x][y] = num;
dfs(x + 1, y, c);
dfs(x - 1, y, c);
dfs(x, y + 1, c);
dfs(x, y - 1, c);
};
for (int x = 0; x < n; ++x) {
for (int y = 0; y < m; ++y) {
if (comp[x][y] || g[x][y] == '.')
continue;
num++;
dfs(x, y, g[x][y]);
}
}
std::vector<int> d(num + 1, 0);
std::queue<int> q;
q.push(1);
d[1] = 1;
int ans = 1;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v : adj[u]) {
if (d[v] == 0) {
q.push(v);
d[v] = d[u] + 1;
ans = std::max(ans, d[v]);
}
}
}
std::cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
10696 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
17 ms |
6732 KB |
Output is correct |
5 |
Correct |
6 ms |
3100 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
444 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
6 ms |
2636 KB |
Output is correct |
11 |
Correct |
4 ms |
2112 KB |
Output is correct |
12 |
Correct |
14 ms |
4064 KB |
Output is correct |
13 |
Correct |
6 ms |
3148 KB |
Output is correct |
14 |
Correct |
6 ms |
3148 KB |
Output is correct |
15 |
Correct |
32 ms |
10248 KB |
Output is correct |
16 |
Correct |
38 ms |
10680 KB |
Output is correct |
17 |
Correct |
25 ms |
9036 KB |
Output is correct |
18 |
Correct |
15 ms |
6680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2636 KB |
Output is correct |
2 |
Correct |
134 ms |
54980 KB |
Output is correct |
3 |
Correct |
795 ms |
517240 KB |
Output is correct |
4 |
Correct |
183 ms |
117188 KB |
Output is correct |
5 |
Correct |
929 ms |
416108 KB |
Output is correct |
6 |
Correct |
1604 ms |
575636 KB |
Output is correct |
7 |
Correct |
4 ms |
2508 KB |
Output is correct |
8 |
Correct |
4 ms |
2636 KB |
Output is correct |
9 |
Correct |
7 ms |
3020 KB |
Output is correct |
10 |
Correct |
2 ms |
1740 KB |
Output is correct |
11 |
Correct |
3 ms |
2380 KB |
Output is correct |
12 |
Correct |
3 ms |
1356 KB |
Output is correct |
13 |
Correct |
135 ms |
54984 KB |
Output is correct |
14 |
Correct |
79 ms |
32040 KB |
Output is correct |
15 |
Correct |
69 ms |
34316 KB |
Output is correct |
16 |
Correct |
67 ms |
23960 KB |
Output is correct |
17 |
Correct |
338 ms |
140788 KB |
Output is correct |
18 |
Correct |
269 ms |
134748 KB |
Output is correct |
19 |
Correct |
183 ms |
117284 KB |
Output is correct |
20 |
Correct |
186 ms |
112696 KB |
Output is correct |
21 |
Correct |
482 ms |
302156 KB |
Output is correct |
22 |
Correct |
946 ms |
416220 KB |
Output is correct |
23 |
Correct |
663 ms |
269248 KB |
Output is correct |
24 |
Correct |
515 ms |
313176 KB |
Output is correct |
25 |
Correct |
1257 ms |
538816 KB |
Output is correct |
26 |
Runtime error |
695 ms |
1048580 KB |
Execution killed with signal 9 |
27 |
Correct |
1596 ms |
862480 KB |
Output is correct |
28 |
Correct |
1611 ms |
574956 KB |
Output is correct |
29 |
Correct |
1540 ms |
554648 KB |
Output is correct |
30 |
Correct |
1576 ms |
627052 KB |
Output is correct |
31 |
Correct |
1722 ms |
427068 KB |
Output is correct |
32 |
Correct |
1763 ms |
901568 KB |
Output is correct |