이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 30000
#define M_ 11111111
int min(int a, int b) { return a < b ? a : b; }
void append(int **eh, int *eo, int i, int h) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
eh[i] = (int *) realloc(eh[i], o * 2 * sizeof *eh[i]);
eh[i][o] = h;
}
int main() {
static int *eh1[N + 1], eo1[N + 1], *eh2[N + 1], eo2[N + 1], ii[M_], jj[M_], hh[N], pp[M_], qq[M_], dd[M_], qu[M_ * 2];
int n, m, m_, h, i, j, o, head, cnt;
scanf("%d%d", &n, &m);
for (i = 0; i <= n; i++) {
eh1[i] = (int *) malloc(2 * sizeof *eh1[i]);
eh2[i] = (int *) malloc(2 * sizeof *eh2[i]);
}
for (h = 0; h < m; h++) {
scanf("%d%d", &i, &j), j = min(j, n);
ii[h] = i, jj[h] = j;
append(eh1, eo1, i, h);
append(eh2, eo2, j, h);
}
m_ = m;
memset(hh, -1, n * sizeof *hh);
memset(pp, -1, sizeof pp), memset(qq, -1, sizeof qq);
for (j = 1; j < n; j++) {
for (o = eo2[j]; o--; ) {
h = eh2[j][o];
hh[ii[h]] = h;
}
for (o = eo2[j]; o--; ) {
h = eh2[j][o];
if (hh[ii[h]] != -1) {
for (i = ii[h] % j; i < n; i += j)
if (hh[i] == -1)
ii[m_] = i, jj[m_] = j, hh[i] = m_++;
for (i = ii[h] % j; i < n; i += j) {
if (i + j < n)
qq[hh[i]] = hh[i + j], pp[hh[i + j]] = hh[i];
hh[i] = -1;
}
}
}
}
for (h = 0; h < m_; h++)
dd[h] = m_;
head = m_, cnt = 0;
dd[0] = 0, qu[head + cnt++] = 0;
while (cnt) {
int d, o;
h = qu[cnt--, head++], i = ii[h], d = dd[h];
if (h == 1) {
printf("%d\n", d);
return 0;
}
for (o = eo1[i]; o--; ) {
int h_ = eh1[i][o];
if (dd[h_] > d)
dd[h_] = d, qu[cnt++, --head] = h_;
}
eo1[i] = 0;
d++;
if (pp[h] != -1 && dd[pp[h]] > d)
dd[pp[h]] = d, qu[head + cnt++] = pp[h];
if (qq[h] != -1 && dd[qq[h]] > d)
dd[qq[h]] = d, qu[head + cnt++] = qq[h];
}
printf("-1\n");
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
skyscraper.c: In function 'append':
skyscraper.c:13:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
13 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
skyscraper.c: In function 'main':
skyscraper.c:19:71: warning: variable 'jj' set but not used [-Wunused-but-set-variable]
19 | static int *eh1[N + 1], eo1[N + 1], *eh2[N + 1], eo2[N + 1], ii[M_], jj[M_], hh[N], pp[M_], qq[M_], dd[M_], qu[M_ * 2];
| ^~
skyscraper.c:22:2: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
22 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
skyscraper.c:28:3: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
28 | scanf("%d%d", &i, &j), j = min(j, n);
| ^~~~~~~~~~~~~~~~~~~~~| # | 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... |