#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 state {
int dist, y, x;
};
struct ds {
int par[inf * inf], siz[inf * inf];
inline int find(int u) {
if (u == par[u]) return u;
return par[u] = find(par[u]);
}
inline 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;
memset(vis, -1, sizeof(vis));
queue<state> q;
for (int i = 0; i < m; i++) {
int x, y; cin >> x >> y;
cul.siz[--y * n + --x] = 1;
q.push({0, y, x});
vis[y][x] = y * n + x;
}
while (!q.empty()) {
state s = q.front();
int dist = s.dist;
int y = s.y, x = s.x;
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({dist + 1, ny, nx});
vis[ny][nx] = here;
}
}
if (cul.siz[cul.find(here)] == m) {
cout << dist << '\n';
break;
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
16248 KB |
Output is correct |
2 |
Correct |
16 ms |
16248 KB |
Output is correct |
3 |
Correct |
17 ms |
16248 KB |
Output is correct |
4 |
Correct |
16 ms |
16216 KB |
Output is correct |
5 |
Correct |
16 ms |
16252 KB |
Output is correct |
6 |
Correct |
16 ms |
16248 KB |
Output is correct |
7 |
Correct |
16 ms |
16248 KB |
Output is correct |
8 |
Correct |
16 ms |
16312 KB |
Output is correct |
9 |
Correct |
16 ms |
16248 KB |
Output is correct |
10 |
Correct |
15 ms |
16248 KB |
Output is correct |
11 |
Correct |
16 ms |
16248 KB |
Output is correct |
12 |
Correct |
17 ms |
16248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
16248 KB |
Output is correct |
2 |
Correct |
16 ms |
16248 KB |
Output is correct |
3 |
Correct |
17 ms |
16248 KB |
Output is correct |
4 |
Correct |
16 ms |
16216 KB |
Output is correct |
5 |
Correct |
16 ms |
16252 KB |
Output is correct |
6 |
Correct |
16 ms |
16248 KB |
Output is correct |
7 |
Correct |
16 ms |
16248 KB |
Output is correct |
8 |
Correct |
16 ms |
16312 KB |
Output is correct |
9 |
Correct |
16 ms |
16248 KB |
Output is correct |
10 |
Correct |
15 ms |
16248 KB |
Output is correct |
11 |
Correct |
16 ms |
16248 KB |
Output is correct |
12 |
Correct |
116 ms |
24044 KB |
Output is correct |
13 |
Correct |
88 ms |
23932 KB |
Output is correct |
14 |
Correct |
117 ms |
24064 KB |
Output is correct |
15 |
Correct |
88 ms |
24084 KB |
Output is correct |
16 |
Correct |
31 ms |
22392 KB |
Output is correct |
17 |
Correct |
289 ms |
28920 KB |
Output is correct |
18 |
Correct |
297 ms |
28920 KB |
Output is correct |
19 |
Correct |
292 ms |
28920 KB |
Output is correct |
20 |
Correct |
305 ms |
28792 KB |
Output is correct |
21 |
Correct |
299 ms |
29000 KB |
Output is correct |
22 |
Correct |
22 ms |
24056 KB |
Output is correct |
23 |
Correct |
132 ms |
24104 KB |
Output is correct |
24 |
Correct |
17 ms |
16248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
16248 KB |
Output is correct |
2 |
Correct |
16 ms |
16248 KB |
Output is correct |
3 |
Correct |
17 ms |
16248 KB |
Output is correct |
4 |
Correct |
16 ms |
16216 KB |
Output is correct |
5 |
Correct |
16 ms |
16252 KB |
Output is correct |
6 |
Correct |
16 ms |
16248 KB |
Output is correct |
7 |
Correct |
16 ms |
16248 KB |
Output is correct |
8 |
Correct |
16 ms |
16312 KB |
Output is correct |
9 |
Correct |
16 ms |
16248 KB |
Output is correct |
10 |
Correct |
15 ms |
16248 KB |
Output is correct |
11 |
Correct |
16 ms |
16248 KB |
Output is correct |
12 |
Correct |
116 ms |
24044 KB |
Output is correct |
13 |
Correct |
88 ms |
23932 KB |
Output is correct |
14 |
Correct |
117 ms |
24064 KB |
Output is correct |
15 |
Correct |
88 ms |
24084 KB |
Output is correct |
16 |
Correct |
31 ms |
22392 KB |
Output is correct |
17 |
Correct |
289 ms |
28920 KB |
Output is correct |
18 |
Correct |
297 ms |
28920 KB |
Output is correct |
19 |
Correct |
292 ms |
28920 KB |
Output is correct |
20 |
Correct |
305 ms |
28792 KB |
Output is correct |
21 |
Correct |
299 ms |
29000 KB |
Output is correct |
22 |
Correct |
22 ms |
24056 KB |
Output is correct |
23 |
Correct |
132 ms |
24104 KB |
Output is correct |
24 |
Correct |
332 ms |
45272 KB |
Output is correct |
25 |
Correct |
424 ms |
47452 KB |
Output is correct |
26 |
Correct |
297 ms |
46608 KB |
Output is correct |
27 |
Correct |
354 ms |
45764 KB |
Output is correct |
28 |
Correct |
271 ms |
45304 KB |
Output is correct |
29 |
Execution timed out |
1034 ms |
54712 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |