#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 2000
#define M 100000
int max(int a, int b) { return a > b ? a : b; }
int di[] = { -1, 1, 0, 0 };
int dj[] = { 0, 0, -1, 1 };
int *eu[N], *ev[N], eo[N];
void append(int d, int u, int v) {
int o = eo[d]++;
if (o >= 2 && (o & o - 1) == 0) {
eu[d] = (int *) realloc(eu[d], o * 2 * sizeof *eu[d]);
ev[d] = (int *) realloc(ev[d], o * 2 * sizeof *ev[d]);
}
eu[d][o] = u, ev[d][o] = v;
}
int ds[M];
int find(int i) {
return ds[i] < 0 ? i : (ds[i] = find(ds[i]));
}
int join(int i, int j) {
i = find(i);
j = find(j);
if (i == j)
return 0;
if (ds[i] > ds[j])
ds[i] = j;
else {
if (ds[i] == ds[j])
ds[i]--;
ds[j] = i;
}
return 1;
}
int main() {
static int dd[N][N], uu[N][N], qu[N * N];
int n, m, cnt, g, h, i, i_, j, j_, ij, ij_, u, v, d, o, k;
scanf("%d%d", &n, &m);
if (m == 1) {
printf("0\n");
return 0;
}
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
dd[i][j] = n * 2 + 1;
cnt = 0;
for (u = 0; u < m; u++) {
scanf("%d%d", &i, &j), i--, j--;
dd[i][j] = 0, uu[i][j] = u, qu[cnt++] = i * n + j;
}
for (h = 0; h < cnt; h++) {
ij = qu[h], i = ij / n, j = ij % n, d = dd[i][j] + 1, u = uu[i][j];
for (g = 0; g < 4; g++) {
i_ = i + di[g], j_ = j + dj[g], ij_ = i_ * n + j_;
if (i_ >= 0 && i_ < n && j_ >= 0 && j_ < n && dd[i_][j_] > d)
dd[i_][j_] = d, uu[i_][j_] = u, qu[cnt++] = ij_;
}
}
for (d = 0; d < n; d++) {
eu[d] = (int *) malloc(2 * sizeof *eu[d]);
ev[d] = (int *) malloc(2 * sizeof *ev[d]);
}
for (i = 0; i < n; i++)
for (j = 0; j < n; j++) {
u = uu[i][j];
for (g = 0; g < 4; g += 2) {
i_ = i + di[g], j_ = j + dj[g], v = uu[i_][j_];
if (i_ >= 0 && i_ < n && j_ >= 0 && j_ < n && u != v) {
d = max(dd[i][j], dd[i_][j_]);
if (d < n)
append(d, u, v);
}
}
}
memset(ds, -1, m * sizeof *ds);
k = m;
for (d = 0; d < n; d++)
for (o = eo[d]; o--; ) {
u = eu[d][o], v = ev[d][o];
if (join(u, v) && --k == 1) {
printf("%d\n", d);
return 0;
}
}
return 0;
}
Compilation message
civilization.cpp: In function 'void append(int, int, int)':
civilization.cpp:18:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
18 | if (o >= 2 && (o & o - 1) == 0) {
| ~~^~~
civilization.cpp: In function 'int main()':
civilization.cpp:50:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
civilization.cpp:60:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | scanf("%d%d", &i, &j), i--, j--;
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6572 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6568 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6576 KB |
Output is correct |
10 |
Correct |
2 ms |
6744 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6572 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6568 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6576 KB |
Output is correct |
10 |
Correct |
2 ms |
6744 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
25 ms |
25756 KB |
Output is correct |
14 |
Correct |
30 ms |
25940 KB |
Output is correct |
15 |
Correct |
27 ms |
25692 KB |
Output is correct |
16 |
Correct |
25 ms |
25692 KB |
Output is correct |
17 |
Correct |
24 ms |
25692 KB |
Output is correct |
18 |
Correct |
82 ms |
33164 KB |
Output is correct |
19 |
Correct |
96 ms |
33324 KB |
Output is correct |
20 |
Correct |
78 ms |
33164 KB |
Output is correct |
21 |
Correct |
74 ms |
33236 KB |
Output is correct |
22 |
Correct |
80 ms |
33376 KB |
Output is correct |
23 |
Correct |
37 ms |
33876 KB |
Output is correct |
24 |
Correct |
41 ms |
33880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6572 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6568 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6576 KB |
Output is correct |
10 |
Correct |
2 ms |
6744 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
25 ms |
25756 KB |
Output is correct |
13 |
Correct |
30 ms |
25940 KB |
Output is correct |
14 |
Correct |
27 ms |
25692 KB |
Output is correct |
15 |
Correct |
25 ms |
25692 KB |
Output is correct |
16 |
Correct |
24 ms |
25692 KB |
Output is correct |
17 |
Correct |
82 ms |
33164 KB |
Output is correct |
18 |
Correct |
96 ms |
33324 KB |
Output is correct |
19 |
Correct |
78 ms |
33164 KB |
Output is correct |
20 |
Correct |
74 ms |
33236 KB |
Output is correct |
21 |
Correct |
80 ms |
33376 KB |
Output is correct |
22 |
Correct |
37 ms |
33876 KB |
Output is correct |
23 |
Correct |
41 ms |
33880 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
102 ms |
47440 KB |
Output is correct |
26 |
Correct |
94 ms |
47368 KB |
Output is correct |
27 |
Correct |
94 ms |
47348 KB |
Output is correct |
28 |
Correct |
96 ms |
47352 KB |
Output is correct |
29 |
Correct |
97 ms |
47352 KB |
Output is correct |
30 |
Correct |
420 ms |
57284 KB |
Output is correct |
31 |
Correct |
390 ms |
52492 KB |
Output is correct |
32 |
Correct |
505 ms |
61716 KB |
Output is correct |
33 |
Correct |
488 ms |
61496 KB |
Output is correct |
34 |
Correct |
497 ms |
61204 KB |
Output is correct |
35 |
Correct |
117 ms |
68948 KB |
Output is correct |
36 |
Correct |
108 ms |
63572 KB |
Output is correct |