Submission #140587

#TimeUsernameProblemLanguageResultExecution timeMemory
140587khrbuddy03문명 (KOI17_civilization)C++14
54 / 100
1041 ms54540 KiB
#include <bits/stdc++.h> using namespace std; const int inf = 2e3 + 9; const int dx[4] = {-1, 1, 0, 0}; const int dy[4] = {0, 0, -1, 1}; struct ds { int par[inf * inf], siz[inf * inf]; int find(int u) { if (u == par[u]) return u; return par[u] = find(par[u]); } void merge(int u, int v) { u = find(u); v = find(v); if (u == v) return; par[u] = v; siz[v] += siz[u]; siz[u] = 0; } } cul; int n, m; int vis[inf][inf]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 0; i < n * n; i++) cul.par[i] = i; fill(&cul.siz[0], &cul.siz[n * n - 1], 0); fill(&vis[0][0], &vis[n - 1][n - 1], -1); queue<pair<int, pair<int, int>>> q; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; cul.siz[--y * n + --x] = 1; q.push(make_pair(0, make_pair(y, x))); vis[y][x] = y * n + x; } while (!q.empty()) { int dist = q.front().first; int y = q.front().second.first, x = q.front().second.second; int here = y * n + x; q.pop(); cul.merge(here, vis[y][x]); for (int i = 0; i < 4; i++) { int ny = y + dy[i], nx = x + dx[i]; if (ny < 0 || ny >= n || nx < 0 || nx >= n) continue; int there = ny * n + nx; if (cul.siz[cul.find(there)] && cul.siz[cul.find(here)]) cul.merge(there, here); if (vis[ny][nx] == -1) { q.push(make_pair(dist + 1, make_pair(ny, nx))); vis[ny][nx] = here; } } if (cul.siz[cul.find(here)] == m) { cout << dist << '\n'; break; } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...