This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define SZ(x) (int)(x.size())
#define F0(i,n) for(int i=0;i<n;i++)
#define F1(i,n) for(int i=1;i<=n;i++)
#define CL(a,x) memset(x, a, sizeof(x));
#define PR(x) cerr << #x << "=" << (x) << endl;
const int MOD = 1000000007;
const double pi = atan(1.0)*4.0;
const double eps = 1e-8;
const int N = 1000001;
int i, j, k, m, n;
int a[N];
int b[N], d[N], c[N];
vector<pii> v;
int clbest, clsum, cnt;
ll ans;
const int DX[]={-1,0,1,0,0};
const int DY[]={0,1,0,-1,0};
void nosol() {
cout << "No" << endl;
exit(0);
}
bool Empty(int x, int y) {
if (x < 0 || x >= m || y < 0 || y >= n) return 0;
return !c[x*n + y];
}
int GetA(int x, int y) {
return a[x*n + y];
}
void Set(int x, int y, int val) {
c[x*n + y] = val;
}
void Fill(int x, int y) {
Set(x, y, 1);
ans += GetA(x, y);
}
void DFS(int x, int y) {
F0(k, 4) if (Empty(x + DX[k], y + DY[k])) {
cnt++;
if (clbest == -1 || clbest > GetA(x + DX[k], y + DY[k])) {
clbest = GetA(x + DX[k], y + DY[k]);
}
Fill(x + DX[k], y + DY[k]);
}
F0(k, 4) {
int x2 = x + 2 * DX[k], y2 = y + 2 * DY[k];
if (Empty(x2, y2) && b[x2*n + y2]) {
Fill(x2, y2);
v.push_back(pii(x2, y2));
DFS(x2, y2);
}
}
}
int main() {
//freopen("x.in", "r", stdin);
cin >> m >> n;
F0(i, m * n) scanf("%d", &a[i]);
cin >> k;
while (k--) {
scanf("%d%d", &i, &j);
b[i*n+j] = 1;
}
// find pairs
F0(i, m) F0(j, n) if (b[i*n + j]) {
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) if (dx || dy) {
int i2 = i + dx, j2 = j + dy;
if (i2 < 0 || i2 >= m || j2 < 0 || j2 >= n || !b[i2*n + j2]) continue;
if (d[i*n + j] && d[i2*n + j2]) continue;
if (d[i*n + j] || d[i2*n + j2]) nosol();
d[i*n + j] = 1;
d[i2*n + j2] = 1;
F0(k, 5) if (!Empty(i + DX[k], j + DY[k])) nosol();
F0(k, 5) if (!Empty(i2 + DX[k], j2 + DY[k])) nosol();
F0(k, 5) if (Empty(i + DX[k], j + DY[k])) Fill(i + DX[k], j + DY[k]);
F0(k, 5) if (Empty(i2 + DX[k], j2 + DY[k])) Fill(i2 + DX[k], j2 + DY[k]);
}
}
}
F0(i, m) F0(j, n) if (b[i*n + j] && !d[i*n + j]) {
if (!Empty(i, j)) nosol();
int found = 0;
F0(k, 4) {
int good = 1;
F0(dir, 4) if (dir != k) {
if (!Empty(i + DX[dir], j + DY[dir])) {
good = 0;
break;
}
}
found = 1;
break;
}
if (!found) nosol();
}
F0(i, m) F0(j, n) if (b[i*n + j] && Empty(i, j)) {
clbest = -1;
cnt = 0;
v.clear();
v.push_back(pii(i, j));
Fill(i, j);
DFS(i, j);
//PR(cnt);
// PR(SZ(v));
if (cnt < 3*SZ(v)) nosol();
else if (cnt == 3*SZ(v) + 1) ans -= clbest;
}
cout << ans << endl;
return 0;
}
Compilation message (stderr)
covering.cpp: In function 'int main()':
covering.cpp:97:17: warning: variable 'good' set but not used [-Wunused-but-set-variable]
int good = 1;
^~~~
covering.cpp:69:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
F0(i, m * n) scanf("%d", &a[i]);
~~~~~^~~~~~~~~~~~~
covering.cpp:72:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &i, &j);
~~~~~^~~~~~~~~~~~~~~~
# | 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... |