#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, -1));
std::vector<std::vector<int>> adj;
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] != -1 && comp[x][y] != num) {
adj[comp[x][y]].push_back(num);
adj[num].push_back(comp[x][y]);
}
if (comp[x][y] != -1 || 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] != -1 || g[x][y] == '.')
continue;
adj.push_back({});
dfs(x, y, g[x][y]);
num++;
}
}
std::vector<int> d(num, 0);
std::queue<int> q;
q.push(0);
d[0] = 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
6140 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
13 ms |
3084 KB |
Output is correct |
5 |
Correct |
5 ms |
1228 KB |
Output is correct |
6 |
Correct |
1 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 |
5 ms |
1228 KB |
Output is correct |
11 |
Correct |
4 ms |
1100 KB |
Output is correct |
12 |
Correct |
13 ms |
2144 KB |
Output is correct |
13 |
Correct |
4 ms |
1228 KB |
Output is correct |
14 |
Correct |
5 ms |
1228 KB |
Output is correct |
15 |
Correct |
30 ms |
5312 KB |
Output is correct |
16 |
Correct |
35 ms |
6084 KB |
Output is correct |
17 |
Correct |
22 ms |
4484 KB |
Output is correct |
18 |
Correct |
13 ms |
3084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1484 KB |
Output is correct |
2 |
Correct |
115 ms |
22452 KB |
Output is correct |
3 |
Correct |
655 ms |
174880 KB |
Output is correct |
4 |
Correct |
141 ms |
34660 KB |
Output is correct |
5 |
Correct |
988 ms |
374500 KB |
Output is correct |
6 |
Correct |
1441 ms |
220012 KB |
Output is correct |
7 |
Correct |
3 ms |
1612 KB |
Output is correct |
8 |
Correct |
3 ms |
1484 KB |
Output is correct |
9 |
Correct |
5 ms |
1740 KB |
Output is correct |
10 |
Correct |
2 ms |
1100 KB |
Output is correct |
11 |
Correct |
2 ms |
1356 KB |
Output is correct |
12 |
Correct |
3 ms |
1100 KB |
Output is correct |
13 |
Correct |
119 ms |
22500 KB |
Output is correct |
14 |
Correct |
70 ms |
13192 KB |
Output is correct |
15 |
Correct |
58 ms |
15760 KB |
Output is correct |
16 |
Correct |
59 ms |
10744 KB |
Output is correct |
17 |
Correct |
306 ms |
57224 KB |
Output is correct |
18 |
Correct |
227 ms |
61852 KB |
Output is correct |
19 |
Correct |
141 ms |
34628 KB |
Output is correct |
20 |
Correct |
150 ms |
40164 KB |
Output is correct |
21 |
Correct |
402 ms |
103096 KB |
Output is correct |
22 |
Correct |
980 ms |
374800 KB |
Output is correct |
23 |
Correct |
590 ms |
110136 KB |
Output is correct |
24 |
Correct |
464 ms |
134400 KB |
Output is correct |
25 |
Correct |
1118 ms |
246056 KB |
Output is correct |
26 |
Runtime error |
763 ms |
1048580 KB |
Execution killed with signal 9 |
27 |
Correct |
1394 ms |
487112 KB |
Output is correct |
28 |
Correct |
1405 ms |
219752 KB |
Output is correct |
29 |
Correct |
1357 ms |
186700 KB |
Output is correct |
30 |
Correct |
1407 ms |
264624 KB |
Output is correct |
31 |
Correct |
1629 ms |
218352 KB |
Output is correct |
32 |
Correct |
1533 ms |
530908 KB |
Output is correct |