#include <bits/stdc++.h>
using namespace std;
using ll = long long;
signed main() {
cin.tie(0)->sync_with_stdio(0);
int n, m;
cin >> n >> m;
vector<string> mat(n);
for (string& s : mat) {
cin >> s;
}
auto id = [m](int i, int j) { return i* m + j;};
vector<vector<pair<int,int>>> adj(n * m);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
for (auto [di, dj] : {pair{0, -1}, {0, 1}, {-1, 0}, {1, 0}}) {
int ni = i + di;
int nj = j + dj;
if (clamp(ni, 0,n-1) == ni && clamp(nj, 0, m-1) == nj) {
if (mat[ni][nj] == '.' || mat[i][j] == '.') continue;
int c = mat[i][j] != mat[ni][nj];
adj[id(i, j)].push_back({id(ni, nj), c});
}
}
}
}
static constexpr int inf = 1e9;
vector<int> dist(n*m, inf), vis(n*m, 0);
int res = 0;
vector<int> edge, cur;
vector<int> stk;
stk.reserve(1e9);
auto dfs = [&](int i, int d) -> void {
stk.clear();
stk.push_back(i);
while (stk.size()) {
int i = stk.back();
stk.pop_back();
vis[i] = 1;
dist[i] = d;
for (auto [e, ec]: adj[i]) {
if (vis[e]) continue;
if (ec) {vis[e] = 1, edge.push_back(e);}
else
stk.push_back(e);
}
res = max(res, dist[i]);
}
};
dfs(0, 1);
for (int d = 2; edge.size(); d ++) {
cur.clear();
swap(cur, edge);
for (int k : cur)
dfs(k, d);
}
cout << res << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
19176 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
32 ms |
12712 KB |
Output is correct |
5 |
Correct |
6 ms |
3796 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
720 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
7 ms |
3540 KB |
Output is correct |
11 |
Correct |
8 ms |
3572 KB |
Output is correct |
12 |
Correct |
19 ms |
7056 KB |
Output is correct |
13 |
Correct |
7 ms |
3724 KB |
Output is correct |
14 |
Correct |
7 ms |
3796 KB |
Output is correct |
15 |
Correct |
39 ms |
16808 KB |
Output is correct |
16 |
Correct |
53 ms |
19148 KB |
Output is correct |
17 |
Correct |
30 ms |
12500 KB |
Output is correct |
18 |
Correct |
34 ms |
12716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2388 KB |
Output is correct |
2 |
Correct |
155 ms |
79192 KB |
Output is correct |
3 |
Correct |
915 ms |
671992 KB |
Output is correct |
4 |
Correct |
248 ms |
142176 KB |
Output is correct |
5 |
Correct |
739 ms |
432908 KB |
Output is correct |
6 |
Runtime error |
1927 ms |
1048576 KB |
Execution killed with signal 9 |
7 |
Correct |
4 ms |
2132 KB |
Output is correct |
8 |
Correct |
4 ms |
2388 KB |
Output is correct |
9 |
Correct |
6 ms |
3412 KB |
Output is correct |
10 |
Correct |
3 ms |
1620 KB |
Output is correct |
11 |
Correct |
3 ms |
2132 KB |
Output is correct |
12 |
Correct |
3 ms |
1364 KB |
Output is correct |
13 |
Correct |
165 ms |
79184 KB |
Output is correct |
14 |
Correct |
88 ms |
46028 KB |
Output is correct |
15 |
Correct |
89 ms |
41724 KB |
Output is correct |
16 |
Correct |
76 ms |
36764 KB |
Output is correct |
17 |
Correct |
424 ms |
202956 KB |
Output is correct |
18 |
Correct |
342 ms |
164392 KB |
Output is correct |
19 |
Correct |
275 ms |
142252 KB |
Output is correct |
20 |
Correct |
217 ms |
147696 KB |
Output is correct |
21 |
Correct |
546 ms |
393428 KB |
Output is correct |
22 |
Correct |
739 ms |
432932 KB |
Output is correct |
23 |
Correct |
776 ms |
392520 KB |
Output is correct |
24 |
Correct |
570 ms |
388928 KB |
Output is correct |
25 |
Correct |
1874 ms |
661904 KB |
Output is correct |
26 |
Execution timed out |
2104 ms |
991036 KB |
Time limit exceeded |
27 |
Runtime error |
1752 ms |
1048576 KB |
Execution killed with signal 9 |
28 |
Runtime error |
1735 ms |
1048576 KB |
Execution killed with signal 9 |
29 |
Runtime error |
1654 ms |
1048576 KB |
Execution killed with signal 9 |
30 |
Runtime error |
1708 ms |
1048576 KB |
Execution killed with signal 9 |
31 |
Execution timed out |
2113 ms |
812256 KB |
Time limit exceeded |
32 |
Runtime error |
1657 ms |
1048576 KB |
Execution killed with signal 9 |