이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/* 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<ll> 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];
ll 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, y = u%n;
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+10; r++) {
grid[r].resize(n+10);
seen[r].resize(n+10);
mark[r].resize(n+10);
for (int c = 0; c < n+10; 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 = 0; x < m; x++) {
for (int y = 0; y < n; y++) {
if (mark[x][y]) {
int u = f(x,y);
if (vis[u]) continue;
t++;
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... |