제출 #592739

#제출 시각아이디문제언어결과실행 시간메모리
592739rainboyStranded Far From Home (BOI22_island)C11
100 / 100
235 ms39476 KiB
#include <stdio.h> #include <stdlib.h> #include <string.h> #define N 200000 unsigned int X = 12345; int rand_() { return (X *= 3) >> 1; } int aa[N]; void sort(int *ii, int l, int r) { while (l < r) { int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp; while (j < k) if (aa[ii[j]] == aa[i_]) j++; else if (aa[ii[j]] < aa[i_]) { tmp = ii[i], ii[i] = ii[j], ii[j] = tmp; i++, j++; } else { k--; tmp = ii[j], ii[j] = ii[k], ii[k] = tmp; } sort(ii, l, i); l = k; } } int *ej[N], eo[N], *ej_[N], eo_[N]; void append(int **ej, int *eo, int i, int j) { int o = eo[i]++; if (o >= 2 && (o & o - 1) == 0) ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]); ej[i][o] = j; } int ds[N], rr[N]; int find(int i) { return ds[i] < 0 ? i : (ds[i] = find(ds[i])); } void join(int i, int j) { i = find(i), j = find(j); if (i == j || rr[j] == -1) return; append(ej_, eo_, rr[i], rr[j]); if (ds[i] > ds[j]) ds[i] = j, rr[j] = rr[i]; else { if (ds[i] == ds[j]) ds[i]--; ds[j] = i; } } long long sz[N]; void dfs1(int i) { int o; sz[i] = aa[i]; for (o = eo_[i]; o--; ) { int j = ej_[i][o]; dfs1(j); sz[i] += sz[j]; } } char cc[N + 1]; void dfs2(int i) { int o; cc[i] = 1; for (o = eo_[i]; o--; ) { int j = ej_[i][o]; if (sz[j] >= aa[i]) dfs2(j); } } int main() { static int ii[N]; int n, m, h, i, j, o; scanf("%d%d", &n, &m); for (i = 0; i < n; i++) scanf("%d", &aa[i]); for (i = 0; i < n; i++) ii[i] = i; sort(ii, 0, n); for (i = 0; i < n; i++) ej[i] = (int *) malloc(2 * sizeof *ej[i]); while (m--) { scanf("%d%d", &i, &j), i--, j--; append(ej, eo, i, j), append(ej, eo, j, i); } memset(ds, -1, n * sizeof *ds), memset(rr, -1, n * sizeof *rr); for (i = 0; i < n; i++) ej_[i] = (int *) malloc(2 * sizeof *ej_[i]); for (h = 0; h < n; h++) { i = ii[h]; rr[i] = i; for (o = eo[i]; o--; ) { j = ej[i][o]; join(i, j); } } i = rr[find(0)]; dfs1(i), dfs2(i); for (i = 0; i < n; i++) cc[i] += '0'; printf("%s\n", cc); return 0; }

컴파일 시 표준 에러 (stderr) 메시지

island.c: In function 'append':
island.c:39:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   39 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
island.c: In function 'main':
island.c:96:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |  scanf("%d%d", &n, &m);
      |  ^~~~~~~~~~~~~~~~~~~~~
island.c:98:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   98 |   scanf("%d", &aa[i]);
      |   ^~~~~~~~~~~~~~~~~~~
island.c:105:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  105 |   scanf("%d%d", &i, &j), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~
#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...