Submission #439246

#TimeUsernameProblemLanguageResultExecution timeMemory
439246elazarkorenT-Covering (eJOI19_covering)C++17
55 / 100
277 ms12100 KiB
#include <iostream> #include <vector> #include <algorithm> #include <set> #define x first #define y second #define chkmin(a, b) a = min(a, b) #define chkmax(a, b) a = max(a, b) using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; typedef vector<pii> vii; typedef vector<bool> vb; typedef vector<vb> vvb; const int infinity = 1e9; int dir_x[] = {-1, 1, 0, 0, -1, -1, 1, 1}; int dir_y[] = {0, 0, -1, 1, -1, 1, -1, 1}; set<pii> touch; int ans = 0; bool Dfs(vvi &graph, int node, vvi &board, vii &special, int parent, vb &visited) { int x = special[node].x, y = special[node].y; ans += board[x][y]; touch.insert({x - 1, y}), touch.insert({x + 1, y}), touch.insert({x, y + 1}), touch.insert({x, y - 1}); visited[node] = true; for (int neighbor : graph[node]) { if (neighbor == parent) continue; if (visited[neighbor]) return false; if (!Dfs(graph, neighbor, board, special, node, visited)) return false; } return true; } int main() { int n, m; cin >> n >> m; vvi board(n, vi(m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> board[i][j]; } } int k; cin >> k; vii special(k); vvi visited(n, vi(m)); for (int i = 0; i < k; i++) { cin >> special[i].x >> special[i].y; visited[special[i].x][special[i].y] = i + 1; } vb finished(k); vvi graph(k); for (int i = 0; i < k; i++) { int x = special[i].x, y = special[i].y; int sum = board[x][y], bad = 4; if (x && !visited[x - 1][y]) { sum += board[x - 1][y]; bad--; } if (x < n - 1 && !visited[x + 1][y]) { sum += board[x + 1][y]; bad--; } if (y && !visited[x][y - 1]) { sum += board[x][y - 1]; bad--; } if (y < m - 1 && !visited[x][y + 1]) { sum += board[x][y + 1]; bad--; } int bad2 = 0; for (int j = 4; j < 8; j++) { x += dir_x[j], y += dir_y[j]; if (0 <= x && x < n && 0 <= y && y < m && visited[x][y]) { bad2++; } x -= dir_x[j], y -= dir_y[j]; } if (bad + bad2 > 1) { cout << "No"; return 0; } else if (bad == 1 && !bad2) { finished[i] = true; ans += sum; } else { if (bad2) { touch.insert({x, y}), touch.insert({x - 1, y}), touch.insert({x + 1, y}), touch.insert({x, y + 1}), touch.insert({x, y - 1}); finished[i] = true; } else { if (x > 1 && visited[x - 2][y]) { graph[i].push_back(visited[x - 2][y] - 1); } if (y > 1 && visited[x][y - 2]) { graph[i].push_back(visited[x][y - 2] - 1); } if (x < n - 2 && visited[x + 2][y]) { graph[i].push_back(visited[x + 2][y] - 1); } if (y < m - 2 && visited[x][y + 2]) { graph[i].push_back(visited[x][y + 2] - 1); } } } } for (pii p : touch) { ans += board[p.x][p.y]; } touch.clear(); for (int i = 0; i < k; i++) { if (finished[i]) continue; if (!Dfs(graph, i, board, special, -1, finished)) { cout << "No"; return 0; } int min_val = infinity; for (pii p : touch) { ans += board[p.x][p.y]; chkmin(min_val, board[p.x][p.y]); } ans -= min_val; touch.clear(); } cout << ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...