#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define pii pair<int, int>
#define print(x) cout<<"["<<(x)<<"]"
const int N = 1e6 + 5;
const pii E[4] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
const pii V[4] = {{-1, -1}, {-1, 1}, {1, 1}, {1, -1}};
const pii dt[4] = {{-2, 0}, {0, 2}, {2, 0}, {0, -2}};
int n , m , k;
vector<vector<int>> a, dfs_vis, cycle;
vector<vector<bool>> vis, red;
vector<vector<vector<pii>>> adj;
vector<vector<pii>> par;
set<pii> cycle_hit;
vector<pii> tree;
pii L[N];
void stop(){cout << "No"; exit(0);}
bool boundary(int i, int j){return (i == 1 || i == n || j == 1 || j == m);}
bool inside(int i, int j){return (1 <= i && i <= n && 1 <= j && j <= m);}
bool corner(int i, int j){return ((i == 1 || i == n) && (j == 1 || j == m));}
void dfs(int i_, int j_, int i, int j){
if (dfs_vis[i][j] == 2) return;
if (dfs_vis[i][j] == 1){
pii tmp = {i_, j_};
cycle[tmp.fi][tmp.se]++;
while (!(tmp.fi == i && tmp.se == j)){
pii xx = par[tmp.fi][tmp.se];
tmp.fi = xx.fi;
tmp.se = xx.se;
cycle[tmp.fi][tmp.se]++;
}
return;
}
dfs_vis[i][j] = 1;
par[i][j] = {i_, j_};
for (auto c: adj[i][j]){
if (c == par[i][j]) continue;
dfs(i, j, c.fi, c.se);
}
dfs_vis[i][j] = 2;
}
int cnt;
void dfs2(int i_, int j_, int i, int j){
cnt++;
for (int t = 0; t < 4; t++){
int u = i + E[t].fi;
int v = j + E[t].se;
if (!vis[u][v]) tree.pb({u, v});
}
dfs_vis[i][j] = 3;
for (auto c: adj[i][j]){
if (!red[c.fi][c.se] || (c.fi == i_ && c.se == j_))
continue;
dfs2(i, j, c.fi, c.se);
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
a.resize(n + 2, vector<int>(m + 2));
dfs_vis.resize(n + 2, vector<int>(m + 2, 0));
vis.resize(n + 2, vector<bool>(m + 2, false));
red.resize(n + 2, vector<bool>(m + 2, false));
cycle.resize(n + 2, vector<int>(m + 2, 0));
adj.resize(n + 2, vector<vector<pii>>(m + 2));
par.resize(n + 2, vector<pii>(m + 2));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) cin >> a[i][j];
cin >> k;
for (int i = 1; i <= k; i++){
cin >> L[i].fi >> L[i].se;
L[i].fi++; L[i].se++;
int u = L[i].fi, v = L[i].se;
vis[u][v] = true; red[u][v] = true;
}
for (int i = 1; i <= k; i++){
int u = L[i].fi, v = L[i].se;
if (boundary(u, v)){
if (corner(u, v)) stop();
for (int t = 0; t < 4; t++){
int u_ = u + E[t].fi;
int v_ = v + E[t].se;
if (inside(u_, v_)){
if (vis[u_][v_]) stop();
vis[u_][v_] = true;
}
}
red[u][v] = false;
}
}
// boundary-> done
for (int u = 1; u <= n; u++)
for (int v = 1; v <= m; v++){
if (red[u][v]){
for (int t = 1; t <= 2; t++){
int u_ = u + E[t].fi;
int v_ = v + E[t].se;
if (red[u_][v_]){
set<pii> tmp;
tmp.insert({u, v}); tmp.insert({u_, v_});
for (int tt = 0; tt < 4; tt++){
int u__ = u_ + E[tt].fi;
int v__ = v_ + E[tt].se;
tmp.insert({u__, v__});
}
for (int tt = 0; tt < 4; tt++){
int _u_ = u + E[tt].fi;
int _v_ = v + E[tt].se;
tmp.insert({_u_, _v_});
}
if (tmp.size() != 8) stop();
for (auto c: tmp){
if (!(c.fi == u && c.se == v) &&
!(c.fi == u_ && c.se == v_) && vis[c.fi][c.se]) stop();
vis[c.fi][c.se] = true;
}
red[u][v] = red[u_][v_] = false;
}
}
}
}
// edge -> done
for (int u = 1; u <= n; u++)
for (int v = 1; v <= m; v++){
if (red[u][v]){
for (int t = 1; t <= 2; t++){
int u_ = u + V[t].fi;
int v_ = v + V[t].se;
if (red[u_][v_]){
set<pii> tmp;
tmp.insert({u, v}); tmp.insert({u_, v_});
for (int tt = 0; tt < 4; tt++){
int u__ = u_ + E[tt].fi;
int v__ = v_ + E[tt].se;
tmp.insert({u__, v__});
}
for (int tt = 0; tt < 4; tt++){
int _u_ = u + E[tt].fi;
int _v_ = v + E[tt].se;
tmp.insert({_u_, _v_});
}
if (tmp.size() != 8) stop();
for (auto c: tmp){
if (!(c.fi == u && c.se == v) &&
!(c.fi == u_ && c.se == v_) && vis[c.fi][c.se]) stop();
vis[c.fi][c.se] = true;
}
red[u][v] = red[u_][v_] = false;
}
}
}
}
// vertice -> done
for (int u = 1; u <= n; u++)
for (int v = 1; v <= m; v++){
if (red[u][v]){
for (int t = 0; t < 4; t++){
int u_ = u + dt[t].fi;
int v_ = v + dt[t].se;
if (!inside(u_, v_)) continue;
if (red[u_][v_]) adj[u][v].pb({u_, v_});
}
}
}
for (int u = 1; u <= n; u++)
for (int v = 1; v <= m; v++){
if (red[u][v] && dfs_vis[u][v] != 2){
dfs(0, 0, u, v);
}
}
for (int u = 1; u <= n; u++)
for (int v = 1; v <= m; v++)
if (cycle[u][v]){
if (cycle[u][v] > 1) stop();
for (int t = 0; t < 4; t++){
int u_ = u + E[t].fi;
int v_ = v + E[t].se;
cycle_hit.insert({u_, v_});
}
red[u][v] = false;
}
for (auto c: cycle_hit){
if (vis[c.fi][c.se]) stop();
vis[c.fi][c.se] = true;
}
// cycle -> done;
for (int u = 1; u <= n; u++)
for (int v = 1; v <= m; v++){
if (red[u][v] && dfs_vis[u][v] != 3){
tree.clear(); cnt = 0;
dfs2(0, 0, u, v);
sort(tree.begin(), tree.end());
tree.erase(unique(tree.begin(), tree.end()), tree.end());
int Tr = (int)tree.size();
if (Tr < 3 * cnt || Tr > 3 * cnt + 1) stop();
if (Tr == 3 * cnt + 1){
sort(tree.begin(), tree.end(), [&](pii x, pii y){
return a[x.fi][x.se] < a[y.fi][y.se];
});
for (int i = 1; i < tree.size(); i++)
vis[tree[i].fi][tree[i].se] = true;
}else{
for (auto c: tree)
vis[c.fi][c.se] = true;
}
}
}
int ans = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (vis[i][j]) ans += a[i][j];
/*
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
if (vis[i][j]) print("x");
else print("o");
}
cout << '\n';
}
*/
cout << ans;
}
Compilation message
covering.cpp: In function 'int main()':
covering.cpp:205:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
205 | for (int i = 1; i < tree.size(); i++)
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
3 ms |
1748 KB |
Output is correct |
3 |
Correct |
8 ms |
4692 KB |
Output is correct |
4 |
Correct |
26 ms |
13628 KB |
Output is correct |
5 |
Correct |
81 ms |
44052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
724 KB |
Output is correct |
2 |
Correct |
3 ms |
1748 KB |
Output is correct |
3 |
Correct |
9 ms |
4692 KB |
Output is correct |
4 |
Correct |
28 ms |
13612 KB |
Output is correct |
5 |
Correct |
88 ms |
44084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
3 ms |
1748 KB |
Output is correct |
3 |
Correct |
9 ms |
4692 KB |
Output is correct |
4 |
Correct |
27 ms |
13564 KB |
Output is correct |
5 |
Correct |
84 ms |
44088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
4 ms |
2900 KB |
Output is correct |
4 |
Correct |
3 ms |
1848 KB |
Output is correct |
5 |
Correct |
9 ms |
5680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
3 ms |
1748 KB |
Output is correct |
3 |
Correct |
8 ms |
4692 KB |
Output is correct |
4 |
Correct |
26 ms |
13628 KB |
Output is correct |
5 |
Correct |
81 ms |
44052 KB |
Output is correct |
6 |
Correct |
2 ms |
724 KB |
Output is correct |
7 |
Correct |
3 ms |
1748 KB |
Output is correct |
8 |
Correct |
9 ms |
4692 KB |
Output is correct |
9 |
Correct |
28 ms |
13612 KB |
Output is correct |
10 |
Correct |
88 ms |
44084 KB |
Output is correct |
11 |
Correct |
1 ms |
724 KB |
Output is correct |
12 |
Correct |
3 ms |
1748 KB |
Output is correct |
13 |
Correct |
9 ms |
4692 KB |
Output is correct |
14 |
Correct |
27 ms |
13564 KB |
Output is correct |
15 |
Correct |
84 ms |
44088 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
4 ms |
2900 KB |
Output is correct |
19 |
Correct |
3 ms |
1848 KB |
Output is correct |
20 |
Correct |
9 ms |
5680 KB |
Output is correct |
21 |
Correct |
1 ms |
724 KB |
Output is correct |
22 |
Correct |
3 ms |
1636 KB |
Output is correct |
23 |
Correct |
10 ms |
4692 KB |
Output is correct |
24 |
Correct |
27 ms |
13592 KB |
Output is correct |
25 |
Correct |
82 ms |
44092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
3 ms |
1748 KB |
Output is correct |
3 |
Correct |
8 ms |
4692 KB |
Output is correct |
4 |
Correct |
26 ms |
13628 KB |
Output is correct |
5 |
Correct |
81 ms |
44052 KB |
Output is correct |
6 |
Correct |
2 ms |
724 KB |
Output is correct |
7 |
Correct |
3 ms |
1748 KB |
Output is correct |
8 |
Correct |
9 ms |
4692 KB |
Output is correct |
9 |
Correct |
28 ms |
13612 KB |
Output is correct |
10 |
Correct |
88 ms |
44084 KB |
Output is correct |
11 |
Correct |
1 ms |
724 KB |
Output is correct |
12 |
Correct |
3 ms |
1748 KB |
Output is correct |
13 |
Correct |
9 ms |
4692 KB |
Output is correct |
14 |
Correct |
27 ms |
13564 KB |
Output is correct |
15 |
Correct |
84 ms |
44088 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
4 ms |
2900 KB |
Output is correct |
19 |
Correct |
3 ms |
1848 KB |
Output is correct |
20 |
Correct |
9 ms |
5680 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
3 ms |
1636 KB |
Output is correct |
28 |
Correct |
10 ms |
4692 KB |
Output is correct |
29 |
Correct |
27 ms |
13592 KB |
Output is correct |
30 |
Correct |
82 ms |
44092 KB |
Output is correct |
31 |
Correct |
232 ms |
76632 KB |
Output is correct |
32 |
Correct |
80 ms |
44616 KB |
Output is correct |
33 |
Correct |
112 ms |
52696 KB |
Output is correct |
34 |
Correct |
83 ms |
44764 KB |
Output is correct |
35 |
Correct |
139 ms |
52312 KB |
Output is correct |