제출 #739043

#제출 시각아이디문제언어결과실행 시간메모리
739043rainboyColors (RMI18_colors)C11
100 / 100
712 ms20448 KiB
#include <stdio.h> #include <stdlib.h> #include <string.h> #define N 150000 #define M 200000 unsigned int X = 12345; int rand_() { return (X *= 3) >> 1; } int *ej[N], eo[N], n; void append(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 aa[N * 2], ii[N * 2]; 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 ds[N]; int find(int i) { return ds[i] < 0 ? i : find(ds[i]); } int ii1[M], jj1[M], xx[M], cnt; void join(int i, int j) { int tmp; j = find(j); i = find(i); if (i == j) { ii1[cnt] = -1, jj1[cnt] = -1, xx[cnt] = 0, cnt++; return; } if (ds[i] > ds[j]) tmp = i, i = j, j = tmp; ii1[cnt] = i, jj1[cnt] = j, xx[cnt] = ds[j], cnt++; ds[i] += ds[j], ds[j] = i; } void undo() { int i, j, x; cnt--; i = ii1[cnt], j = jj1[cnt], x = xx[cnt]; if (i == -1) return; ds[j] = x, ds[i] -= ds[j]; } char active[N]; void activate(int i) { int o; active[i] = 1; for (o = eo[i]; o--; ) { int j = ej[i][o]; if (active[j]) join(i, j); } } void deactivate(int i) { int o; active[i] = 0; for (o = 0; o < eo[i]; o++) { int j = ej[i][o]; if (active[j]) undo(); } } int i_, j_; int dfs(int *ii_, int m, int al, int ar) { static char used[N]; int am, h, h1, h2, h3, i, good, tmp; h1 = 0, h2 = 0, h3 = m; while (h2 < h3) if (aa[ii_[h2] << 1 | 0] <= al && ar <= aa[ii_[h2] << 1 | 1]) h2++; else if (aa[ii_[h2] << 1 | 0] <= ar && al <= aa[ii_[h2] << 1 | 1]) { tmp = ii_[h1], ii_[h1] = ii_[h2], ii_[h2] = tmp; h1++, h2++; } else { h3--; tmp = ii_[h2], ii_[h2] = ii_[h3], ii_[h3] = tmp; } for (h = h1; h < h2; h++) activate(ii_[h]); if (al < ar) { am = (al + ar) / 2; good = dfs(ii_, h1, al, am) && dfs(ii_, h1, am + 1, ar); } else { j_ = i_; while (j_ < n * 2 && aa[ii[j_]] == al) j_++; for (h = i_; h < j_; h++) { i = ii[h]; if ((i & 1) == 1) used[find(i >> 1)] = 1; } good = 1; for (h = i_; h < j_; h++) { i = ii[h]; if ((i & 1) == 0 && !used[find(i >> 1)]) good = 0; } for (h = i_; h < j_; h++) { i = ii[h]; if ((i & 1) == 1) used[find(i >> 1)] = 0; } i_ = j_; } for (h = h2 - 1; h >= h1; h--) deactivate(ii_[h]); return good; } int main() { int t; scanf("%d", &t); while (t--) { static int ii_[N]; int m, h, i, j, yes; scanf("%d%d", &n, &m); for (i = 0; i < n; i++) scanf("%d", &aa[i << 1 | 1]), aa[i << 1 | 1]--; for (i = 0; i < n; i++) scanf("%d", &aa[i << 1 | 0]), aa[i << 1 | 0]--; for (i = 0; i < n; i++) ej[i] = (int *) malloc(2 * sizeof *ej[i]), eo[i] = 0; for (h = 0; h < m; h++) { scanf("%d%d", &i, &j), i--, j--; append(i, j), append(j, i); } yes = 1; for (i = 0; i < n; i++) if (aa[i << 1 | 0] > aa[i << 1 | 1]) yes = 0; if (!yes) { printf("0\n"); for (i = 0; i < n; i++) free(ej[i]); continue; } for (i = 0; i < n * 2; i++) ii[i] = i; sort(ii, 0, n * 2); memset(active, 0, n * sizeof *active), memset(ds, -1, n * sizeof *ds); for (i = 0; i < n; i++) ii_[i] = i; i_ = 0; printf("%d\n", dfs(ii_, n, 0, n - 1)); for (i = 0; i < n; i++) free(ej[i]); } return 0; }

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

colors.c: In function 'append':
colors.c:19:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   19 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
colors.c: In function 'main':
colors.c:156:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  156 |  scanf("%d", &t);
      |  ^~~~~~~~~~~~~~~
colors.c:161:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  161 |   scanf("%d%d", &n, &m);
      |   ^~~~~~~~~~~~~~~~~~~~~
colors.c:163:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  163 |    scanf("%d", &aa[i << 1 | 1]), aa[i << 1 | 1]--;
      |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
colors.c:165:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  165 |    scanf("%d", &aa[i << 1 | 0]), aa[i << 1 | 0]--;
      |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
colors.c:169:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  169 |    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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...