This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* In the name of God, aka Allah */
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mk make_pair
#define endl '\n'
#define kill(x) if (x) return cout << "No" << endl, void()
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll inf = 1e9 + 50;
const int N = 1e6 + 50;
int m, n, k;
vector<int> grid[N];
vector<bool> mark[N];
vector<bool> seen[N];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int dx2[] = {-1, 1, 1, -1};
int dy2[] = {1, 1, -1, -1};
ll ans;
int t;
int bk[N];
int mn[N];
vector<int> adj[N];
bool vis[N];
bool val(int x, int y) {
return x >= 1 && x <= m && y >= 1 && y <= n;
}
int f(int x, int y) {
return (x-1)*n + y;
}
void dfs(int u, int p) {
vis[u] = 1;
int x = (u+n-1)/n, y = (u+n-1)%n + 1;
seen[x][y] = 1;
ans += grid[x][y];
for (int i = 0; i < 4; i++) {
if (seen[x+dx[i]][y+dy[i]]) continue;
seen[x+dx[i]][y+dy[i]] = 1;
ans += grid[x+dx[i]][y+dy[i]];
mn[t] = min(grid[x+dx[i]][y+dy[i]], mn[t]);
}
for (int v:adj[u]) {
if (v == p) continue;
if (!vis[v]) dfs(v, u);
else if (vis[v] == 1) bk[t]++;
}
vis[u] = 2;
}
void solve() {
cin >> m >> n;
for (int r = 0; r < m+5; r++) {
grid[r].resize(n+5);
seen[r].resize(n+5);
mark[r].resize(n+5);
for (int c = 0; c < n+5; c++) {
grid[r][c] = -inf;
}
}
for (int r = 1; r <= m; r++) {
for (int c = 1; c <= n; c++) {
cin >> grid[r][c];
}
}
cin >> k;
for (int i = 1; i <= k; i++) {
int r,c;
cin >> r >> c;
r++, c++;
mark[r][c] = 1;
}
// DOJOB
for (int x = 1; x <= m; x++) {
for (int y = 1; y <= n; y++) {
for (int i = 0; i < 4; i++) {
if (val(x+2*dx[i], y+2*dy[i]) && mark[x][y] && mark[x+2*dx[i]][y+2*dy[i]]) {
int u = f(x, y);
int v = f(x+2*dx[i], y+2*dy[i]);
adj[u].push_back(v);
}
}
}
}
for (int x = 1; x <= m; x++) {
for (int y = 1; y <= n; y++) {
if (mark[x][y]) {
int u = f(x,y);
if (vis[u]) continue;
t++;
mn[t] = inf;
dfs(u, u);
kill(bk[t] >= 2);
if (!bk[t]) {
ans -= mn[t];
}
}
}
}
kill(ans < 0);
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |