#include <iostream>
#include <set>
#include <vector>
using i32 = int32_t;
struct UnionFind {
std::vector<i32> parent;
explicit UnionFind(size_t sz) : parent(sz, -1) {}
i32 find(i32 u) {
if (parent[u] < 0)
return u;
else
return parent[u] = find(parent[u]);
}
bool unite(i32 u, i32 v) {
u = find(u);
v = find(v);
if (u == v) {
return false;
} else {
if (-parent[u] < -parent[v])
std::swap(u, v);
parent[u] += parent[v];
parent[v] = u;
return false;
}
}
};
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
i32 h, w;
std::cin >> h >> w;
std::vector<std::string> grid(h);
for (i32 i = 0; i < h; i++)
std::cin >> grid[i];
UnionFind dsu(h * w);
i32 idx = 0;
for (i32 r = 0; r < h; r++) {
const auto &row = grid[r];
for (i32 c = 0; c < w; c++, idx++) {
if (c + 1 < w && grid[r][c] == grid[r][c + 1]) {
dsu.unite(idx, idx + 1);
}
if (r + 1 < h && grid[r][c] == grid[r + 1][c]) {
dsu.unite(idx, idx + w);
}
}
}
std::vector<std::set<i32>> adj(h * w);
auto adge = [&](i32 u, i32 v) {
adj[dsu.find(u)].insert(dsu.find(v));
adj[dsu.find(v)].insert(dsu.find(u));
};
idx = 0;
for (i32 r = 0; r < h; r++) {
const auto &row = grid[r];
for (i32 c = 0; c < w; c++, idx++) {
if (row[c] == '.') continue;
char op = row[c] == 'F' ? 'R' : 'F';
if (c + 1 < w && grid[r][c + 1] == op) {
adge(idx, idx + 1);
}
if (r + 1 < h && grid[r + 1][c] == op) {
adge(idx, idx + w);
}
}
}
std::vector<i32> next;
std::vector<i32> front { dsu.find(0) };
std::vector<bool> visited(h * w, false);
visited[0] = true;
int t;
for (t = 0; !front.empty(); t++) {
for (int x : front) {
for (int neighbour : adj[x]) {
if (!visited[neighbour]) {
visited[neighbour] = true;
next.push_back(neighbour);
}
}
}
std::swap(next, front);
next.clear();
}
std::cout << t;
}
Compilation message
tracks.cpp: In function 'int main()':
tracks.cpp:51:17: warning: unused variable 'row' [-Wunused-variable]
51 | const auto &row = grid[r];
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
18036 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
10 ms |
8532 KB |
Output is correct |
5 |
Correct |
5 ms |
5460 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
852 KB |
Output is correct |
10 |
Correct |
5 ms |
4564 KB |
Output is correct |
11 |
Correct |
3 ms |
2516 KB |
Output is correct |
12 |
Correct |
12 ms |
6740 KB |
Output is correct |
13 |
Correct |
6 ms |
5460 KB |
Output is correct |
14 |
Correct |
5 ms |
5544 KB |
Output is correct |
15 |
Correct |
29 ms |
17956 KB |
Output is correct |
16 |
Correct |
38 ms |
18000 KB |
Output is correct |
17 |
Correct |
23 ms |
16944 KB |
Output is correct |
18 |
Correct |
10 ms |
8616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3412 KB |
Output is correct |
2 |
Correct |
156 ms |
103608 KB |
Output is correct |
3 |
Correct |
1104 ms |
996316 KB |
Output is correct |
4 |
Correct |
257 ms |
222016 KB |
Output is correct |
5 |
Correct |
910 ms |
891044 KB |
Output is correct |
6 |
Correct |
1282 ms |
889040 KB |
Output is correct |
7 |
Correct |
4 ms |
3028 KB |
Output is correct |
8 |
Correct |
3 ms |
3412 KB |
Output is correct |
9 |
Correct |
5 ms |
4052 KB |
Output is correct |
10 |
Correct |
2 ms |
2132 KB |
Output is correct |
11 |
Correct |
2 ms |
3028 KB |
Output is correct |
12 |
Correct |
2 ms |
2516 KB |
Output is correct |
13 |
Correct |
138 ms |
103700 KB |
Output is correct |
14 |
Correct |
83 ms |
60112 KB |
Output is correct |
15 |
Correct |
104 ms |
65608 KB |
Output is correct |
16 |
Correct |
67 ms |
44208 KB |
Output is correct |
17 |
Correct |
355 ms |
268292 KB |
Output is correct |
18 |
Correct |
431 ms |
260352 KB |
Output is correct |
19 |
Correct |
264 ms |
221900 KB |
Output is correct |
20 |
Correct |
212 ms |
213524 KB |
Output is correct |
21 |
Correct |
569 ms |
574536 KB |
Output is correct |
22 |
Correct |
880 ms |
891240 KB |
Output is correct |
23 |
Correct |
686 ms |
514044 KB |
Output is correct |
24 |
Correct |
593 ms |
614476 KB |
Output is correct |
25 |
Correct |
1603 ms |
1048576 KB |
Output is correct |
26 |
Correct |
514 ms |
638532 KB |
Output is correct |
27 |
Correct |
574 ms |
835776 KB |
Output is correct |
28 |
Correct |
1350 ms |
889068 KB |
Output is correct |
29 |
Correct |
1165 ms |
866252 KB |
Output is correct |
30 |
Correct |
960 ms |
839664 KB |
Output is correct |
31 |
Correct |
1871 ms |
750664 KB |
Output is correct |
32 |
Correct |
633 ms |
847460 KB |
Output is correct |