#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) {vis[e] = 1, 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 |
63 ms |
19528 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
35 ms |
13260 KB |
Output is correct |
5 |
Correct |
7 ms |
3796 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
708 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
6 ms |
3664 KB |
Output is correct |
11 |
Correct |
9 ms |
3788 KB |
Output is correct |
12 |
Correct |
20 ms |
7164 KB |
Output is correct |
13 |
Correct |
7 ms |
3796 KB |
Output is correct |
14 |
Correct |
8 ms |
3796 KB |
Output is correct |
15 |
Correct |
45 ms |
17012 KB |
Output is correct |
16 |
Correct |
50 ms |
19648 KB |
Output is correct |
17 |
Correct |
29 ms |
12692 KB |
Output is correct |
18 |
Correct |
31 ms |
13224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2548 KB |
Output is correct |
2 |
Correct |
168 ms |
80332 KB |
Output is correct |
3 |
Correct |
949 ms |
673976 KB |
Output is correct |
4 |
Correct |
255 ms |
143384 KB |
Output is correct |
5 |
Correct |
759 ms |
434520 KB |
Output is correct |
6 |
Runtime error |
1640 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 |
7 ms |
3716 KB |
Output is correct |
10 |
Correct |
3 ms |
1748 KB |
Output is correct |
11 |
Correct |
3 ms |
2260 KB |
Output is correct |
12 |
Correct |
4 ms |
1364 KB |
Output is correct |
13 |
Correct |
151 ms |
79700 KB |
Output is correct |
14 |
Correct |
91 ms |
46460 KB |
Output is correct |
15 |
Correct |
96 ms |
42072 KB |
Output is correct |
16 |
Correct |
83 ms |
37152 KB |
Output is correct |
17 |
Correct |
402 ms |
203476 KB |
Output is correct |
18 |
Correct |
392 ms |
165104 KB |
Output is correct |
19 |
Correct |
260 ms |
142860 KB |
Output is correct |
20 |
Correct |
223 ms |
148344 KB |
Output is correct |
21 |
Correct |
612 ms |
394688 KB |
Output is correct |
22 |
Correct |
767 ms |
433968 KB |
Output is correct |
23 |
Correct |
786 ms |
393688 KB |
Output is correct |
24 |
Correct |
606 ms |
390076 KB |
Output is correct |
25 |
Correct |
1925 ms |
663044 KB |
Output is correct |
26 |
Runtime error |
1557 ms |
1048576 KB |
Execution killed with signal 9 |
27 |
Runtime error |
1678 ms |
1048576 KB |
Execution killed with signal 9 |
28 |
Runtime error |
1695 ms |
1048576 KB |
Execution killed with signal 9 |
29 |
Runtime error |
1698 ms |
1048576 KB |
Execution killed with signal 9 |
30 |
Runtime error |
1699 ms |
1048576 KB |
Execution killed with signal 9 |
31 |
Execution timed out |
2071 ms |
812840 KB |
Time limit exceeded |
32 |
Runtime error |
1627 ms |
1048576 KB |
Execution killed with signal 9 |