# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
730891 | rainboy | Biochips (IZhO12_biochips) | C11 | 593 ms | 15328 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |