#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));
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 || 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]);
}
}
g.clear();
std::vector<std::vector<int>> adj(num + 1);
auto addcomp=[&](int i, int j) {
auto add=[&](int x, int y) {
if (x < 0 || x >= n || y < 0 || y >= m)
return;
if (comp[x][y] && comp[x][y] != comp[i][j]) {
adj[comp[x][y]].push_back(comp[i][j]);
adj[comp[i][j]].push_back(comp[x][y]);
}
};
add(i - 1, j);
add(i + 1, j);
add(i, j + 1);
add(i, j - 1);
};
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (comp[i][j])
addcomp(i, j);
}
}
comp.clear();
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 |
44 ms |
7588 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
14 ms |
2968 KB |
Output is correct |
5 |
Correct |
5 ms |
1228 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
6 ms |
1228 KB |
Output is correct |
11 |
Correct |
5 ms |
972 KB |
Output is correct |
12 |
Correct |
16 ms |
2856 KB |
Output is correct |
13 |
Correct |
5 ms |
1228 KB |
Output is correct |
14 |
Correct |
5 ms |
1220 KB |
Output is correct |
15 |
Correct |
36 ms |
6200 KB |
Output is correct |
16 |
Correct |
46 ms |
7564 KB |
Output is correct |
17 |
Correct |
26 ms |
4184 KB |
Output is correct |
18 |
Correct |
14 ms |
2888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1356 KB |
Output is correct |
2 |
Correct |
141 ms |
24492 KB |
Output is correct |
3 |
Correct |
772 ms |
168904 KB |
Output is correct |
4 |
Correct |
176 ms |
31096 KB |
Output is correct |
5 |
Correct |
1067 ms |
282468 KB |
Output is correct |
6 |
Correct |
1546 ms |
218516 KB |
Output is correct |
7 |
Correct |
3 ms |
1356 KB |
Output is correct |
8 |
Correct |
3 ms |
1356 KB |
Output is correct |
9 |
Correct |
6 ms |
1612 KB |
Output is correct |
10 |
Correct |
2 ms |
844 KB |
Output is correct |
11 |
Correct |
3 ms |
1228 KB |
Output is correct |
12 |
Correct |
5 ms |
972 KB |
Output is correct |
13 |
Correct |
162 ms |
24524 KB |
Output is correct |
14 |
Correct |
83 ms |
14404 KB |
Output is correct |
15 |
Correct |
67 ms |
12288 KB |
Output is correct |
16 |
Correct |
75 ms |
12528 KB |
Output is correct |
17 |
Correct |
363 ms |
61804 KB |
Output is correct |
18 |
Correct |
266 ms |
46676 KB |
Output is correct |
19 |
Correct |
171 ms |
31168 KB |
Output is correct |
20 |
Correct |
181 ms |
38628 KB |
Output is correct |
21 |
Correct |
452 ms |
100088 KB |
Output is correct |
22 |
Correct |
1072 ms |
282568 KB |
Output is correct |
23 |
Correct |
712 ms |
119884 KB |
Output is correct |
24 |
Correct |
506 ms |
118600 KB |
Output is correct |
25 |
Correct |
1294 ms |
188856 KB |
Output is correct |
26 |
Runtime error |
868 ms |
1048580 KB |
Execution killed with signal 9 |
27 |
Correct |
1280 ms |
382648 KB |
Output is correct |
28 |
Correct |
1548 ms |
218412 KB |
Output is correct |
29 |
Correct |
1453 ms |
192820 KB |
Output is correct |
30 |
Correct |
1552 ms |
237132 KB |
Output is correct |
31 |
Execution timed out |
2093 ms |
308120 KB |
Time limit exceeded |
32 |
Correct |
1358 ms |
409128 KB |
Output is correct |