#include <bits/stdc++.h>
using namespace std;
int h, w;
const int MAXN = 1.1e6;
int parent[MAXN];
int sz[MAXN];
int highest[MAXN];
vector<vector<bool>> aa, bb;
int get(int i, int j) {
return w * i + j;
}
void init() {
for (int i = 0; i < h; i ++)
for (int j = 0; j < w; j ++) {
int xx = get(i, j);
parent[xx] = xx;
sz[xx] = 1;
highest[xx] = i;
}
}
int find(int u) {
if (parent[u] == u)
return u;
return parent[u] = find(parent[u]);
}
int find(int i, int j) {
return find(get(i, j));
}
void unite(int a, int b) {
a = find(a); b = find(b);
if (a == b) return;
if (sz[a] > sz[b])
swap(a, b);
highest[b] = max(highest[b], highest[a]);
sz[b] += sz[a];
parent[a] = b;
}
void solve() {
// set up components
init();
for (int i = 0; i < h; i ++)
for (int j = 0; j < w - 1; j ++)
if (aa[i][j])
unite(get(i, j), get(i, j + 1));
for (int i = 0; i < h - 1; i ++)
for (int j = 0; j < w; j ++)
if (bb[i][j])
unite(get(i, j), get(i + 1, j));
int t; cin >> t;
set<pair<int, int>> components;
for (int i = 0; i < t; i ++) {
int x, y; cin >> x >> y;
x --; y --;
int xx = find(x, y);
components.insert({highest[xx], xx});
}
// while not all together, merge components
int cost = 0;
int last = -1;
while (components.size() > 1) {
pair<int, int> cur = *components.begin();
components.erase(cur);
int i = cur.first;
for (int j = 0; j < w; j ++) {
int xx = find(i, j);
int curheight = highest[xx];
components.erase({curheight, xx});
}
// merge the top row
for (int j = 0; j < w - 1; j ++)
unite(find(i, j), find(i, j + 1));
// now add back
cost ++;
components.insert({highest[find(i, 0)], find(i, 0)});
if (last == i) {
cost = -1;
break;
}
last = i;
}
cout << cost << endl;
}
int main() {
cin >> h >> w;
int q; cin >> q;
aa.resize(h);
for (int i = 0; i < h; i ++) {
aa[i].resize(w - 1);
string s; cin >> s;
for (int j = 0; j < w - 1; j ++)
aa[i][j] = (s[j] == '1') ? 1 : 0;
}
bb.resize(h - 1);
for (int i = 0; i < h - 1; i ++) {
bb[i].resize(w);
string s; cin >> s;
for (int j = 0; j < w; j ++)
bb[i][j] = (s[j] == '1') ? 1 : 0;
}
for (int i = 0; i < h; i ++) {
int cc; cin >> cc;
}
while (q --)
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
203 ms |
86868 KB |
Output is correct |
3 |
Correct |
73 ms |
14548 KB |
Output is correct |
4 |
Correct |
49 ms |
15188 KB |
Output is correct |
5 |
Correct |
79 ms |
15124 KB |
Output is correct |
6 |
Correct |
54 ms |
15196 KB |
Output is correct |
7 |
Correct |
168 ms |
86616 KB |
Output is correct |
8 |
Correct |
198 ms |
86612 KB |
Output is correct |
9 |
Correct |
178 ms |
86656 KB |
Output is correct |
10 |
Correct |
69 ms |
14868 KB |
Output is correct |
11 |
Correct |
147 ms |
62800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
203 ms |
86868 KB |
Output is correct |
3 |
Correct |
73 ms |
14548 KB |
Output is correct |
4 |
Correct |
49 ms |
15188 KB |
Output is correct |
5 |
Correct |
79 ms |
15124 KB |
Output is correct |
6 |
Correct |
54 ms |
15196 KB |
Output is correct |
7 |
Correct |
168 ms |
86616 KB |
Output is correct |
8 |
Correct |
198 ms |
86612 KB |
Output is correct |
9 |
Correct |
178 ms |
86656 KB |
Output is correct |
10 |
Correct |
69 ms |
14868 KB |
Output is correct |
11 |
Correct |
147 ms |
62800 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
220 ms |
86816 KB |
Output is correct |
14 |
Correct |
81 ms |
34132 KB |
Output is correct |
15 |
Correct |
88 ms |
15188 KB |
Output is correct |
16 |
Correct |
56 ms |
15188 KB |
Output is correct |
17 |
Correct |
61 ms |
15188 KB |
Output is correct |
18 |
Correct |
201 ms |
86612 KB |
Output is correct |
19 |
Correct |
182 ms |
86600 KB |
Output is correct |
20 |
Correct |
105 ms |
14540 KB |
Output is correct |
21 |
Correct |
142 ms |
62808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
203 ms |
86868 KB |
Output is correct |
3 |
Correct |
73 ms |
14548 KB |
Output is correct |
4 |
Correct |
49 ms |
15188 KB |
Output is correct |
5 |
Correct |
79 ms |
15124 KB |
Output is correct |
6 |
Correct |
54 ms |
15196 KB |
Output is correct |
7 |
Correct |
168 ms |
86616 KB |
Output is correct |
8 |
Correct |
198 ms |
86612 KB |
Output is correct |
9 |
Correct |
178 ms |
86656 KB |
Output is correct |
10 |
Correct |
69 ms |
14868 KB |
Output is correct |
11 |
Correct |
147 ms |
62800 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
220 ms |
86816 KB |
Output is correct |
14 |
Correct |
81 ms |
34132 KB |
Output is correct |
15 |
Correct |
88 ms |
15188 KB |
Output is correct |
16 |
Correct |
56 ms |
15188 KB |
Output is correct |
17 |
Correct |
61 ms |
15188 KB |
Output is correct |
18 |
Correct |
201 ms |
86612 KB |
Output is correct |
19 |
Correct |
182 ms |
86600 KB |
Output is correct |
20 |
Correct |
105 ms |
14540 KB |
Output is correct |
21 |
Correct |
142 ms |
62808 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
237 ms |
86612 KB |
Output is correct |
24 |
Correct |
213 ms |
69204 KB |
Output is correct |
25 |
Correct |
290 ms |
18604 KB |
Output is correct |
26 |
Correct |
247 ms |
18064 KB |
Output is correct |
27 |
Correct |
309 ms |
18804 KB |
Output is correct |
28 |
Correct |
273 ms |
90584 KB |
Output is correct |
29 |
Correct |
246 ms |
90708 KB |
Output is correct |
30 |
Correct |
220 ms |
90680 KB |
Output is correct |
31 |
Correct |
208 ms |
86616 KB |
Output is correct |
32 |
Correct |
251 ms |
97504 KB |
Output is correct |
33 |
Correct |
281 ms |
19180 KB |
Output is correct |
34 |
Correct |
170 ms |
62732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
203 ms |
86868 KB |
Output is correct |
3 |
Correct |
73 ms |
14548 KB |
Output is correct |
4 |
Correct |
49 ms |
15188 KB |
Output is correct |
5 |
Correct |
79 ms |
15124 KB |
Output is correct |
6 |
Correct |
54 ms |
15196 KB |
Output is correct |
7 |
Correct |
168 ms |
86616 KB |
Output is correct |
8 |
Correct |
198 ms |
86612 KB |
Output is correct |
9 |
Correct |
178 ms |
86656 KB |
Output is correct |
10 |
Correct |
69 ms |
14868 KB |
Output is correct |
11 |
Correct |
147 ms |
62800 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
220 ms |
86816 KB |
Output is correct |
14 |
Correct |
81 ms |
34132 KB |
Output is correct |
15 |
Correct |
88 ms |
15188 KB |
Output is correct |
16 |
Correct |
56 ms |
15188 KB |
Output is correct |
17 |
Correct |
61 ms |
15188 KB |
Output is correct |
18 |
Correct |
201 ms |
86612 KB |
Output is correct |
19 |
Correct |
182 ms |
86600 KB |
Output is correct |
20 |
Correct |
105 ms |
14540 KB |
Output is correct |
21 |
Correct |
142 ms |
62808 KB |
Output is correct |
22 |
Execution timed out |
3099 ms |
86868 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
203 ms |
86868 KB |
Output is correct |
3 |
Correct |
73 ms |
14548 KB |
Output is correct |
4 |
Correct |
49 ms |
15188 KB |
Output is correct |
5 |
Correct |
79 ms |
15124 KB |
Output is correct |
6 |
Correct |
54 ms |
15196 KB |
Output is correct |
7 |
Correct |
168 ms |
86616 KB |
Output is correct |
8 |
Correct |
198 ms |
86612 KB |
Output is correct |
9 |
Correct |
178 ms |
86656 KB |
Output is correct |
10 |
Correct |
69 ms |
14868 KB |
Output is correct |
11 |
Correct |
147 ms |
62800 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
220 ms |
86816 KB |
Output is correct |
14 |
Correct |
81 ms |
34132 KB |
Output is correct |
15 |
Correct |
88 ms |
15188 KB |
Output is correct |
16 |
Correct |
56 ms |
15188 KB |
Output is correct |
17 |
Correct |
61 ms |
15188 KB |
Output is correct |
18 |
Correct |
201 ms |
86612 KB |
Output is correct |
19 |
Correct |
182 ms |
86600 KB |
Output is correct |
20 |
Correct |
105 ms |
14540 KB |
Output is correct |
21 |
Correct |
142 ms |
62808 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
237 ms |
86612 KB |
Output is correct |
24 |
Correct |
213 ms |
69204 KB |
Output is correct |
25 |
Correct |
290 ms |
18604 KB |
Output is correct |
26 |
Correct |
247 ms |
18064 KB |
Output is correct |
27 |
Correct |
309 ms |
18804 KB |
Output is correct |
28 |
Correct |
273 ms |
90584 KB |
Output is correct |
29 |
Correct |
246 ms |
90708 KB |
Output is correct |
30 |
Correct |
220 ms |
90680 KB |
Output is correct |
31 |
Correct |
208 ms |
86616 KB |
Output is correct |
32 |
Correct |
251 ms |
97504 KB |
Output is correct |
33 |
Correct |
281 ms |
19180 KB |
Output is correct |
34 |
Correct |
170 ms |
62732 KB |
Output is correct |
35 |
Execution timed out |
3099 ms |
86868 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
203 ms |
86868 KB |
Output is correct |
3 |
Correct |
73 ms |
14548 KB |
Output is correct |
4 |
Correct |
49 ms |
15188 KB |
Output is correct |
5 |
Correct |
79 ms |
15124 KB |
Output is correct |
6 |
Correct |
54 ms |
15196 KB |
Output is correct |
7 |
Correct |
168 ms |
86616 KB |
Output is correct |
8 |
Correct |
198 ms |
86612 KB |
Output is correct |
9 |
Correct |
178 ms |
86656 KB |
Output is correct |
10 |
Correct |
69 ms |
14868 KB |
Output is correct |
11 |
Correct |
147 ms |
62800 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
220 ms |
86816 KB |
Output is correct |
14 |
Correct |
81 ms |
34132 KB |
Output is correct |
15 |
Correct |
88 ms |
15188 KB |
Output is correct |
16 |
Correct |
56 ms |
15188 KB |
Output is correct |
17 |
Correct |
61 ms |
15188 KB |
Output is correct |
18 |
Correct |
201 ms |
86612 KB |
Output is correct |
19 |
Correct |
182 ms |
86600 KB |
Output is correct |
20 |
Correct |
105 ms |
14540 KB |
Output is correct |
21 |
Correct |
142 ms |
62808 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
237 ms |
86612 KB |
Output is correct |
24 |
Correct |
213 ms |
69204 KB |
Output is correct |
25 |
Correct |
290 ms |
18604 KB |
Output is correct |
26 |
Correct |
247 ms |
18064 KB |
Output is correct |
27 |
Correct |
309 ms |
18804 KB |
Output is correct |
28 |
Correct |
273 ms |
90584 KB |
Output is correct |
29 |
Correct |
246 ms |
90708 KB |
Output is correct |
30 |
Correct |
220 ms |
90680 KB |
Output is correct |
31 |
Correct |
208 ms |
86616 KB |
Output is correct |
32 |
Correct |
251 ms |
97504 KB |
Output is correct |
33 |
Correct |
281 ms |
19180 KB |
Output is correct |
34 |
Correct |
170 ms |
62732 KB |
Output is correct |
35 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
36 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
203 ms |
86868 KB |
Output is correct |
3 |
Correct |
73 ms |
14548 KB |
Output is correct |
4 |
Correct |
49 ms |
15188 KB |
Output is correct |
5 |
Correct |
79 ms |
15124 KB |
Output is correct |
6 |
Correct |
54 ms |
15196 KB |
Output is correct |
7 |
Correct |
168 ms |
86616 KB |
Output is correct |
8 |
Correct |
198 ms |
86612 KB |
Output is correct |
9 |
Correct |
178 ms |
86656 KB |
Output is correct |
10 |
Correct |
69 ms |
14868 KB |
Output is correct |
11 |
Correct |
147 ms |
62800 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
220 ms |
86816 KB |
Output is correct |
14 |
Correct |
81 ms |
34132 KB |
Output is correct |
15 |
Correct |
88 ms |
15188 KB |
Output is correct |
16 |
Correct |
56 ms |
15188 KB |
Output is correct |
17 |
Correct |
61 ms |
15188 KB |
Output is correct |
18 |
Correct |
201 ms |
86612 KB |
Output is correct |
19 |
Correct |
182 ms |
86600 KB |
Output is correct |
20 |
Correct |
105 ms |
14540 KB |
Output is correct |
21 |
Correct |
142 ms |
62808 KB |
Output is correct |
22 |
Execution timed out |
3099 ms |
86868 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
203 ms |
86868 KB |
Output is correct |
3 |
Correct |
73 ms |
14548 KB |
Output is correct |
4 |
Correct |
49 ms |
15188 KB |
Output is correct |
5 |
Correct |
79 ms |
15124 KB |
Output is correct |
6 |
Correct |
54 ms |
15196 KB |
Output is correct |
7 |
Correct |
168 ms |
86616 KB |
Output is correct |
8 |
Correct |
198 ms |
86612 KB |
Output is correct |
9 |
Correct |
178 ms |
86656 KB |
Output is correct |
10 |
Correct |
69 ms |
14868 KB |
Output is correct |
11 |
Correct |
147 ms |
62800 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
220 ms |
86816 KB |
Output is correct |
14 |
Correct |
81 ms |
34132 KB |
Output is correct |
15 |
Correct |
88 ms |
15188 KB |
Output is correct |
16 |
Correct |
56 ms |
15188 KB |
Output is correct |
17 |
Correct |
61 ms |
15188 KB |
Output is correct |
18 |
Correct |
201 ms |
86612 KB |
Output is correct |
19 |
Correct |
182 ms |
86600 KB |
Output is correct |
20 |
Correct |
105 ms |
14540 KB |
Output is correct |
21 |
Correct |
142 ms |
62808 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
237 ms |
86612 KB |
Output is correct |
24 |
Correct |
213 ms |
69204 KB |
Output is correct |
25 |
Correct |
290 ms |
18604 KB |
Output is correct |
26 |
Correct |
247 ms |
18064 KB |
Output is correct |
27 |
Correct |
309 ms |
18804 KB |
Output is correct |
28 |
Correct |
273 ms |
90584 KB |
Output is correct |
29 |
Correct |
246 ms |
90708 KB |
Output is correct |
30 |
Correct |
220 ms |
90680 KB |
Output is correct |
31 |
Correct |
208 ms |
86616 KB |
Output is correct |
32 |
Correct |
251 ms |
97504 KB |
Output is correct |
33 |
Correct |
281 ms |
19180 KB |
Output is correct |
34 |
Correct |
170 ms |
62732 KB |
Output is correct |
35 |
Execution timed out |
3099 ms |
86868 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |