Submission #482430

#TimeUsernameProblemLanguageResultExecution timeMemory
482430rainboyPapričice (COCI20_papricice)C11
110 / 110
163 ms22892 KiB
#include <stdio.h> #include <stdlib.h> #include <string.h> #define N 200000 int min(int a, int b) { return a < b ? a : b; } int max(int a, int b) { return a > b ? a : b; } int dist(int a, int b, int c) { return max(max(a, b), c) - min(min(a, b), c); } 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 ta[N], tb[N], c; int rr[N], sz[N]; void dfs1(int p, int i) { static int time; int o, centroid; ta[i] = time++; sz[i] = 1, centroid = 1; for (o = eo[i]; o--; ) { int j = ej[i][o]; if (j != p) { dfs1(i, j); sz[i] += sz[j]; if (sz[j] * 2 > n) centroid = 0; } } if ((n - sz[i]) * 2 > n) centroid = 0; if (centroid) c = i; tb[i] = time; } void dfs2(int p, int r, int i) { int o; rr[i] = r, sz[i] = 1; for (o = eo[i]; o--; ) { int j = ej[i][o]; if (j != p) { dfs2(i, r, j); sz[i] += sz[j]; } } } int main() { static int rr_[N], dp1[N], dp2[N], dq1[N], dq2[N]; int h, i, j, s, o, ans; scanf("%d", &n); 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--; append(i, j), append(j, i); } dfs1(-1, 0); for (o = eo[c]; o--; ) { j = ej[c][o]; dfs2(c, j, j); } memset(rr_, -1, n * sizeof *rr_); for (i = 0; i < n; i++) if (rr_[sz[i]] == -1) rr_[sz[i]] = rr[i]; else if (rr_[sz[i]] != rr[i]) rr_[sz[i]] = -2; for (s = 0; s < n; s++) { dp1[s] = s == 0 ? -1 : dp1[s - 1], dp2[s] = s == 0 ? -1 : dp2[s - 1]; if (rr_[s] != -1) { if (dp1[s] == -1 || rr_[dp1[s]] != rr_[s]) dp2[s] = dp1[s]; dp1[s] = s; } } for (s = n - 1; s >= 0; s--) { dq1[s] = s + 1 == n ? n : dq1[s + 1], dq2[s] = s + 1 == n ? n : dq2[s + 1]; if (rr_[s] != -1) { if (dq1[s] == n || rr_[dq1[s]] != rr_[s]) dq2[s] = dq1[s]; dq1[s] = s; } } ans = n + 1; for (i = 0; i < n; i++) if (i != c) { int s, s_; if (i != rr[i]) ans = min(ans, dist(n - sz[rr[i]], sz[rr[i]] - sz[i], sz[i])); s = (n - sz[i]) / 2; if ((s_ = dp1[s]) != -1 && rr_[s_] != rr[i]) ans = min(ans, dist(sz[i], s_, n - sz[i] - s_)); else if ((s_ = dp2[s]) != -1) ans = min(ans, dist(sz[i], s_, n - sz[i] - s_)); if ((s_ = dq1[s]) != n && rr_[s_] != rr[i]) ans = min(ans, dist(sz[i], s_, n - sz[i] - s_)); else if ((s_ = dq2[s]) != n) ans = min(ans, dist(sz[i], s_, n - sz[i] - s_)); } printf("%d\n", ans); return 0; }

Compilation message (stderr)

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