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;
int a,b,n,m,k,l,A[1000006],B[1000006],fix[1000006],ans;
vector <int> v1,v[1000006];
void dfs(int x) {
if(fix[x]==1) return;
fix[x]=1;
if(B[x]==1) l++;
else v1.push_back(-A[x]);
for(int i=0;i<v[x].size();i++) {
dfs(v[x][i]);
}
}
int main() {
cin>>n>>m;
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
cin>>a;
A[(i-1)*m+j]=a;
}
}
cin>>k;
for(int i=1;i<=k;i++) {
cin>>a>>b;
a++; b++;
B[(a-1)*m+b]=1; ans+=A[(a-1)*m+b];
if(a>1) {
v[(a-1)*m+b].push_back((a-2)*m+b);
v[(a-2)*m+b].push_back((a-1)*m+b);
}
if(a<n) {
v[(a-1)*m+b].push_back(a*m+b);
v[a*m+b].push_back((a-1)*m+b);
}
if(b>1) {
v[(a-1)*m+b].push_back((a-1)*m+b-1);
v[(a-1)*m+b-1].push_back((a-1)*m+b);
}
if(b<m) {
v[(a-1)*m+b].push_back((a-1)*m+b+1);
v[(a-1)*m+b+1].push_back((a-1)*m+b+1);
}
}
for(int i=1;i<=n*m;i++) {
if(fix[i]==1) continue;
v1.clear(); l=0;
dfs(i);
if(3*l>v1.size()) { cout<<"No"; return 0; }
sort(v1.begin(),v1.end());
for(int j=0;j<3*l;j++)
ans-=v1[j];
}
cout<<ans;
}
Compilation message (stderr)
covering.cpp: In function 'void dfs(int)':
covering.cpp:10:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
10 | for(int i=0;i<v[x].size();i++) {
| ~^~~~~~~~~~~~
covering.cpp: In function 'int main()':
covering.cpp:48:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | if(3*l>v1.size()) { cout<<"No"; 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... |