이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
vector<pair<int,int>> dirs = {{2,0},{1,-1},{1,0},{1,1},{0,2},{0,1},{0,-1},{0,-2},{-1,-1},{-1,0},{-1,1},{-2,0}};
vector<pair<int,int>> dirs2 = {{1,0},{0,1},{0,-1},{-1,0}};
int main(){
  ios::sync_with_stdio(0);
  cin.tie(0);
  int row,col;
  cin >> row >> col;
  vector<vector<int>> a(row,vector<int>(col));
  for(int i = 0; i < row; ++i){
    for(int j = 0; j < col; ++j) cin >> a[i][j];
  }
  int k,u,v;
  cin >> k;
  map<pair<int,int>,int> mp;
  vector<pair<int,int>> centers;
  for(int i = 0; i < k; ++i){
    cin >> u >> v;
    mp[make_pair(u,v)] = centers.size();
    centers.emplace_back(u,v);
  }
  int nb = centers.size();
  vector<vector<int>> adj(nb);
  for(int i = 0; i < nb; ++i){
    tie(u,v) = centers[i];
    for(auto& dir : dirs){
      int x = u+dir.first,y = v+dir.second;
      if(mp.count(make_pair(x,y))){
        int j = mp[make_pair(x,y)];
        adj[i].push_back(j);
      }
    }
  }
  int ans = 0;
  vector<bool> vu(nb);
  for(int i = 0; i < nb; ++i){
    if(!vu[i]){
      vector<int> cands;
      deque<int> q;
      q.emplace_back(i);
      vu[i] = true;
      while(!q.empty()){
        int node = q.front();
        q.pop_front();
        cands.push_back(node);
        for(int j : adj[node]){
          if(!vu[j]){
            vu[j] = true;
            q.emplace_back(j);
          }
        }
      }
      int tot = cands.size(),around = 0;
      int low = 1e9;
      for(int j : cands){
        tie(u,v) = centers[j];
        ans += a[u][v];
        for(auto dir : dirs2){
          int x = u+dir.first,y = v+dir.second;
          if(0 <= x and x < row and 0 <= y and y < col){
            if(!mp.count(make_pair(x,y))){
              if(a[x][y] >= 0){
                low = min(low,a[x][y]);
                ++around;
                ans += a[x][y];
                a[x][y] = -1;
              }
            }
          }
        }
      }
      if(around < 3*tot){
        cout << "No";
        return 0;
      }
      if(around > 3*tot) ans -= low;
    }
  }
  cout << ans;
  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... |