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;
#define ll long long
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define ff first
#define sc second
#define int ll
const int mod = 1e9 + 7, N = 1e6 + 1;
vector< vector<int> > x;
int n, m;
vector<int> v[N], vis(N), p;
set<int> st;
int cnt = 0, cnt1 = 0;
void dfs(int u){
cnt++;
vis[u] = 1;
if(st.find(u) == st.end()){
int a = u / m;
if(u % m != 0) a++;
int b = u % m;
if(b == 0) b = m;
p.pb(x[a][b]);
}else{
cnt1++;
}
for(auto to : v[u]){
if(vis[to] == 0) dfs(to);
}
}
main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin >> n >> m;
x.resize(n + 1);
for(int i = 1; i <= n; i++){
x[i].resize(m + 1);
for(int j = 1; j <= m; j++){
cin >> x[i][j];
}
}
int k;
cin >> k;
int ans = 0;
for(int i = 1; i <= k; i++){
int a, b;
cin >> a >> b;
a++;b++;
ans += x[a][b];
st.insert((a - 1) * m + b);
vector< pair<int, int> > l = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
for(auto t : l){
int a1 = a + t.ff;
int b1 = b + t.sc;
if(a1 < 1 || a1 > n || b1 < 1 || b1 > m) continue;
v[(a - 1) * m + b].pb((a1 - 1) * m + b1);
v[(a1 - 1) * m + b1].pb((a - 1) * m + b);
}
}
for(auto c : st){
if(vis[c] == 1) continue;
dfs(c);
if(cnt < cnt1 * 4){
cout << "No" << endl;
return 0;
}
sort(rall(p));
for(int i = 0; i < cnt1 * 3; i++) ans += p[i];
cnt = 0;
cnt1 = 0;
p.clear();
}
cout << ans << endl;
}
Compilation message (stderr)
covering.cpp:34:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
34 | main(){
| ^~~~
# | 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... |