Submission #730891

#TimeUsernameProblemLanguageResultExecution timeMemory
730891rainboyBiochips (IZhO12_biochips)C11
100 / 100
593 ms15328 KiB
#include <stdio.h> #include <stdlib.h> #define N 200000 #define INF 0x3f3f3f3f int max(int a, int b) { return a > b ? a : b; } unsigned int X = 12345; int rand_() { return (X *= 3) >> 1; } 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; } int ta[N], tb[N]; void dfs(int i) { static int time; int o; ta[i] = time++; for (o = eo[i]; o--; ) { int j = ej[i][o]; dfs(j); } tb[i] = time; } 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 (tb[ii[j]] == tb[i_]) j++; else if (tb[ii[j]] < tb[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 main() { static int ww[N], ii[N], hh[N], dp[N]; int n, k, h, h_, i, p, r, lower, upper; scanf("%d%d", &n, &k); for (i = 0; i < n; i++) ej[i] = (int *) malloc(2 * sizeof *ej[i]); r = -1; for (i = 0; i < n; i++) { scanf("%d%d", &p, &ww[i]), p--; if (p >= 0) append(p, i); else r = i; } dfs(r); for (i = 0; i < n; i++) ii[i] = i; sort(ii, 0, n); for (h = 0; h < n; h++) { lower = -1, upper = i; while (upper - lower > 1) { h_ = (lower + upper) / 2; if (tb[ii[h_]] <= ta[ii[h]]) lower = h_; else upper = h_; } hh[h] = lower; } for (h = 0; h < n; h++) dp[h] = ww[ii[h]]; for (h = 1; h < n; h++) dp[h] = max(dp[h], dp[h - 1]); while (--k) { for (h = n - 1; h >= 0; h--) dp[h] = hh[h] == -1 || dp[hh[h]] == -INF ? -INF : dp[hh[h]] + ww[ii[h]]; for (h = 1; h < n; h++) dp[h] = max(dp[h], dp[h - 1]); } printf("%d\n", dp[n - 1]); return 0; }

Compilation message (stderr)

biochips.c: In function 'append':
biochips.c:20:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   20 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
biochips.c: In function 'main':
biochips.c:63:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   63 |  scanf("%d%d", &n, &k);
      |  ^~~~~~~~~~~~~~~~~~~~~
biochips.c:68:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   68 |   scanf("%d%d", &p, &ww[i]), p--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...