#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::set<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]].insert(num);
adj[num].insert(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 |
48 ms |
9844 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
15 ms |
3532 KB |
Output is correct |
5 |
Correct |
6 ms |
2056 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
7 ms |
1984 KB |
Output is correct |
11 |
Correct |
4 ms |
1356 KB |
Output is correct |
12 |
Correct |
16 ms |
3396 KB |
Output is correct |
13 |
Correct |
6 ms |
2056 KB |
Output is correct |
14 |
Correct |
7 ms |
2060 KB |
Output is correct |
15 |
Correct |
40 ms |
9160 KB |
Output is correct |
16 |
Correct |
50 ms |
9844 KB |
Output is correct |
17 |
Correct |
33 ms |
8928 KB |
Output is correct |
18 |
Correct |
15 ms |
3532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2252 KB |
Output is correct |
2 |
Correct |
157 ms |
40524 KB |
Output is correct |
3 |
Correct |
809 ms |
304788 KB |
Output is correct |
4 |
Correct |
209 ms |
58964 KB |
Output is correct |
5 |
Correct |
1507 ms |
834520 KB |
Output is correct |
6 |
Execution timed out |
2037 ms |
276280 KB |
Time limit exceeded |
7 |
Correct |
4 ms |
2252 KB |
Output is correct |
8 |
Correct |
4 ms |
2252 KB |
Output is correct |
9 |
Correct |
6 ms |
2568 KB |
Output is correct |
10 |
Correct |
2 ms |
1612 KB |
Output is correct |
11 |
Correct |
3 ms |
1868 KB |
Output is correct |
12 |
Correct |
4 ms |
2052 KB |
Output is correct |
13 |
Correct |
155 ms |
40484 KB |
Output is correct |
14 |
Correct |
91 ms |
23860 KB |
Output is correct |
15 |
Correct |
86 ms |
30252 KB |
Output is correct |
16 |
Correct |
79 ms |
19060 KB |
Output is correct |
17 |
Correct |
372 ms |
104248 KB |
Output is correct |
18 |
Correct |
340 ms |
119524 KB |
Output is correct |
19 |
Correct |
193 ms |
58812 KB |
Output is correct |
20 |
Correct |
185 ms |
70064 KB |
Output is correct |
21 |
Correct |
476 ms |
181076 KB |
Output is correct |
22 |
Correct |
1479 ms |
834704 KB |
Output is correct |
23 |
Correct |
757 ms |
200480 KB |
Output is correct |
24 |
Correct |
609 ms |
263740 KB |
Output is correct |
25 |
Correct |
1510 ms |
476220 KB |
Output is correct |
26 |
Runtime error |
652 ms |
1048580 KB |
Execution killed with signal 9 |
27 |
Correct |
1741 ms |
687396 KB |
Output is correct |
28 |
Correct |
1987 ms |
276324 KB |
Output is correct |
29 |
Correct |
1812 ms |
208800 KB |
Output is correct |
30 |
Correct |
1849 ms |
334728 KB |
Output is correct |
31 |
Execution timed out |
2068 ms |
338764 KB |
Time limit exceeded |
32 |
Correct |
1874 ms |
762572 KB |
Output is correct |