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>
#define MN 717171
#define MM 1007171
#define INF 1000000000
using namespace std;
int n, m, k;
typedef pair<int, int> ii;
//vector<int> A[MN], B[MN];
vector<vector<int> > A, B;
bool V[MM];
deque<int> Nod;
vector<ii> Noduri;
int main() {
cin >> n >> m;
A.assign(n+2, vector<int>(m+2));
B.assign(n+2, vector<int>(m+2));
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j) cin >> A[i][j];
cin >> k;
vector<ii> C(k+2);
vector<vector<int> > L(k+2, vector<int>());
for(int i = 1; i <= k; ++i){
cin >> C[i].first >> C[i].second;
++C[i].first;
++C[i].second;
B[C[i].first][C[i].second] = i;
}
int VX[] = {-2, -1, -1, -1, 0, 0}; //12 / 2
int VY[] = {0, -1, 0, 1, -2, -1};
for(int i = 1; i <= k; ++i){
int ln, cn;
for(int d = 0; d < 6; ++d){
ln = C[i].first + VX[d];
cn = C[i].second + VY[d];
if(ln < 1 || cn < 1 || ln > n || cn > m || !B[ln][cn]) continue;
L[i].push_back(B[ln][cn]);
L[B[ln][cn]].push_back(i);
}
}
int DX[] = {-1, 0, 1, 0};
int DY[] = {0, 1, 0, -1};
int re = 0;
for(int i = 1; i <= k; ++i){
if(V[i]) continue;
V[i] = 1;
Nod.push_back(i);
int acum;
while(!Nod.empty()){
acum = Nod.front();
Nod.pop_front();
Noduri.push_back(C[acum]);
for(auto it : L[acum])
if(!V[it]){
V[it] = 1;
Nod.push_back(it);
}
}
set<ii> Vecini;
int ln, cn;
for(auto it : Noduri) {
re += A[it.first][it.second];
for(int d = 0; d < 4; ++d){
ln = it.first + DX[d];
cn = it.second + DY[d];
if(ln < 1 || cn < 1 || ln > n || cn > m || B[ln][cn])continue;
Vecini.insert({ln, cn});
}
}
for(auto it : Noduri)
Vecini.erase(it);
if(Vecini.size() < Noduri.size() * 3){
cout << "No\n";
return 0;
} else if(Vecini.size() == Noduri.size() * 3){
for(auto it : Vecini)
re += A[it.first][it.second];
} else {
int mi = INF;
for(auto it : Vecini){
re += A[it.first][it.second];
mi = min(mi, A[it.first][it.second]);
}
re -= mi;
}
Noduri.clear();
while(!Nod.empty())Nod.pop_back();
}
cout << re << "\n";
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... |