제출 #481340

#제출 시각아이디문제언어결과실행 시간메모리
481340rainboyLampice (COCI19_lampice)C11
42 / 110
37 ms7888 KiB
#include <stdio.h> #include <stdlib.h> #define N 50000 int abs_(int a) { return a > 0 ? a : -a; } int max(int a, int b) { return a > b ? a : b; } int *ej[N], eo[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; } char cc[N + 1]; int d_; void dfs1(int p1, int i1, int p2, int i2, int d) { int o1, o2; d_ = max(d_, d); for (o1 = eo[i1]; o1--; ) { int j1 = ej[i1][o1]; for (o2 = eo[i2]; o2--; ) { int j2 = ej[i2][o2]; if (j1 != p1 && j2 != p2 && (i1 != i2 || j1 < j2) && cc[j1] == cc[j2]) dfs1(i1, j1, i2, j2, d + 2); } } } int pp[N], dd[N]; void dfs2(int p, int i, int d) { int o; pp[i] = p, dd[i] = d; for (o = eo[i]; o--; ) { int j = ej[i][o]; if (j != p) dfs2(i, j, d + 1); } } int manacher(char *cc, int n) { static char cc_[N * 2 + 2]; static int rr[N * 2 + 1]; int i, o, x, ans; for (i = 0; i < n * 2 + 1; i++) cc_[i] = i % 2 == 0 ? ' ' : cc[i / 2]; for (i = 0, o = 0, x = 0; i < n * 2 + 1; i++) if (i < x && i + rr[o + o - i] < x) rr[i] = rr[o + o - i]; else { o = i, x = max(x, i); while (x < n * 2 + 1 && o + o - x >= 0 && cc_[x] == cc_[o + o - x]) x++; rr[i] = x - o; } ans = 0; for (i = 0; i < n * 2 + 1; i++) ans = max(ans, rr[i] - 1); return ans; } int main() { static int ii[N - 1], jj[N - 1], qu[N], qu1[N], qu2[N]; static char cc_[N + 1]; int n, h, h_, i, j, cnt, cnt1, cnt2; scanf("%d%s", &n, cc); for (i = 0; i < n; i++) ej[i] = (int *) malloc(2 * sizeof *ej[i]); for (h = 0; h < n - 1; h++) { scanf("%d%d", &i, &j), i--, j--; ii[h] = i, jj[h] = j; append(i, j), append(j, i); } if (n <= 3000) { for (i = 0; i < n; i++) dfs1(-1, i, -1, i, 1); for (h = 0; h < n - 1; h++) if (cc[ii[h]] == cc[jj[h]]) dfs1(ii[h], jj[h], jj[h], ii[h], 2); } else { dfs2(-1, 0, 0); cnt = 0; for (i = 0; i < n; i++) if (eo[i] == 1) qu[cnt++] = i; for (h = 0; h < cnt; h++) for (h_ = h + 1; h_ < cnt; h_++) { i = qu[h], j = qu[h_]; cnt1 = 0, cnt2 = 0; while (i != j) if (dd[i] > dd[j]) qu1[cnt1++] = i, i = pp[i]; else qu2[cnt2++] = j, j = pp[j]; for (h = 0; h < cnt1; h++) cc_[h] = cc[qu1[h]]; cc_[cnt1] = cc[i]; for (h = 0; h < cnt2; h++) cc_[cnt1 + cnt2 - h] = cc[qu2[h]]; d_ = max(d_, manacher(cc_, cnt1 + 1 + cnt2)); } } printf("%d\n", d_); return 0; }

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

lampice.c: In function 'append':
lampice.c:14:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   14 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
lampice.c: In function 'main':
lampice.c:78:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   78 |  scanf("%d%s", &n, cc);
      |  ^~~~~~~~~~~~~~~~~~~~~
lampice.c:82:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   82 |   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...