#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;
auto dfs = [&](auto& dfs, int i, int d) -> void {
vis[i] = 1;
dist[i] = d;
for (auto [e, ec]: adj[i]) {
if (vis[e]) continue;
if (ec) {if (!vis[e]) edge.push_back(e);}
else
dfs(dfs, e, d);
}
res = max(res, dist[i]);
};
dfs(dfs, 0, 1);
for (int d = 2; edge.size(); d ++) {
auto cur = std::move(edge);
for (int k : cur)
dfs(dfs, k, d);
}
cout << res << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
19448 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
34 ms |
13216 KB |
Output is correct |
5 |
Correct |
10 ms |
3748 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
6 ms |
3540 KB |
Output is correct |
11 |
Correct |
7 ms |
3796 KB |
Output is correct |
12 |
Correct |
18 ms |
7120 KB |
Output is correct |
13 |
Correct |
8 ms |
3796 KB |
Output is correct |
14 |
Correct |
6 ms |
3796 KB |
Output is correct |
15 |
Correct |
42 ms |
16784 KB |
Output is correct |
16 |
Correct |
54 ms |
19468 KB |
Output is correct |
17 |
Correct |
29 ms |
12572 KB |
Output is correct |
18 |
Correct |
30 ms |
13204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2516 KB |
Output is correct |
2 |
Correct |
158 ms |
79428 KB |
Output is correct |
3 |
Correct |
1014 ms |
672464 KB |
Output is correct |
4 |
Correct |
275 ms |
142796 KB |
Output is correct |
5 |
Correct |
709 ms |
433472 KB |
Output is correct |
6 |
Runtime error |
1612 ms |
1048576 KB |
Execution killed with signal 9 |
7 |
Correct |
4 ms |
2388 KB |
Output is correct |
8 |
Correct |
4 ms |
2644 KB |
Output is correct |
9 |
Correct |
6 ms |
3712 KB |
Output is correct |
10 |
Correct |
2 ms |
1748 KB |
Output is correct |
11 |
Correct |
3 ms |
2384 KB |
Output is correct |
12 |
Correct |
2 ms |
1364 KB |
Output is correct |
13 |
Correct |
187 ms |
79992 KB |
Output is correct |
14 |
Correct |
92 ms |
46800 KB |
Output is correct |
15 |
Correct |
88 ms |
42308 KB |
Output is correct |
16 |
Correct |
83 ms |
37524 KB |
Output is correct |
17 |
Correct |
423 ms |
203784 KB |
Output is correct |
18 |
Correct |
371 ms |
165216 KB |
Output is correct |
19 |
Correct |
248 ms |
142960 KB |
Output is correct |
20 |
Correct |
232 ms |
148508 KB |
Output is correct |
21 |
Correct |
579 ms |
394700 KB |
Output is correct |
22 |
Correct |
723 ms |
434072 KB |
Output is correct |
23 |
Correct |
801 ms |
393732 KB |
Output is correct |
24 |
Correct |
580 ms |
390052 KB |
Output is correct |
25 |
Correct |
1936 ms |
663240 KB |
Output is correct |
26 |
Runtime error |
1483 ms |
1048576 KB |
Execution killed with signal 9 |
27 |
Runtime error |
1767 ms |
1048576 KB |
Execution killed with signal 9 |
28 |
Runtime error |
1603 ms |
1048576 KB |
Execution killed with signal 9 |
29 |
Runtime error |
1592 ms |
1048576 KB |
Execution killed with signal 9 |
30 |
Runtime error |
1629 ms |
1048576 KB |
Execution killed with signal 9 |
31 |
Execution timed out |
2108 ms |
814712 KB |
Time limit exceeded |
32 |
Runtime error |
1522 ms |
1048576 KB |
Execution killed with signal 9 |