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 <stdio.h>
#include <string.h>
#define NM 1000000
#define INF 0x3f3f3f3f
int min(int a, int b) { return a < b ? a : b; }
int di[] = { -1, 1, 0, 0 };
int dj[] = { 0, 0, -1, 1 };
int ds[NM], vv[NM], ee[NM], ww[NM];
int find(int i) {
return ds[i] < 0 ? i : (ds[i] = find(ds[i]));
}
void join(int i, int j) {
i = find(i);
j = find(j);
if (i == j) {
ee[i]++;
return;
}
if (ds[i] > ds[j])
ds[i] = j, vv[j] += vv[i], ee[j] += ee[i] + 1, ww[j] = min(ww[j], ww[i]);
else {
if (ds[i] == ds[j])
ds[i]--;
ds[j] = i, vv[i] += vv[j], ee[i] += ee[j] + 1, ww[i] = min(ww[i], ww[j]);
}
}
int main() {
static int aa[NM], ej[NM][2], eo[NM];
static char special[NM];
int n, m, nm, k, h, i, j, ij;
long long ans;
scanf("%d%d", &n, &m), nm = n * m;
for (i = 0; i < nm; i++)
scanf("%d", &aa[i]);
scanf("%d", &k);
memset(ww, 0x3f, nm * sizeof *ww);
ans = 0;
while (k--) {
scanf("%d%d", &i, &j), ij = i * m + j;
ans += aa[ij];
special[ij] = 1;
aa[ij] = -INF;
}
for (ij = 0; ij < nm; ij++)
if (special[ij]) {
i = ij / m, j = ij % m;
for (h = 0; h < 4; h++) {
int i_ = i + di[h], j_ = j + dj[h], ij_ = i_ * m + j_;
if (i_ >= 0 && i_ < n && j_ >= 0 && j_ < m) {
if (eo[ij_] == 2) {
printf("No\n");
return 0;
}
ej[ij_][eo[ij_]++] = ij;
ans += aa[ij_];
ww[ij] = min(ww[ij], aa[ij_]);
} else {
ans += -INF;
ww[ij] = -INF;
}
}
}
memset(ds, -1, nm * sizeof *ds);
for (i = 0; i < nm; i++)
vv[i] = 1;
for (i = 0; i < nm; i++)
if (eo[i] == 2)
join(ej[i][0], ej[i][1]), ans -= aa[i];
for (i = 0; i < nm; i++)
if (ds[i] < 0) {
if (vv[i] < ee[i]) {
printf("No\n");
return 0;
}
if (vv[i] > ee[i] && ww[i] != INF)
ans -= ww[i];
}
if (ans < 0) {
printf("No\n");
return 0;
}
printf("%lld\n", ans);
return 0;
}
Compilation message (stderr)
covering.c: In function 'main':
covering.c:40:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | scanf("%d%d", &n, &m), nm = n * m;
| ^~~~~~~~~~~~~~~~~~~~~
covering.c:42:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | scanf("%d", &aa[i]);
| ^~~~~~~~~~~~~~~~~~~
covering.c:43:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | scanf("%d", &k);
| ^~~~~~~~~~~~~~~
covering.c:47:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | scanf("%d%d", &i, &j), ij = i * m + 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... |