Submission #72956533
Source Code Expand
Copy
#include <stdio.h>#include <stdlib.h>int cmp(const void* x, const void* y) {int a = *(const int*)x, b = *(const int*)y;return (a > b) - (a < b);}int find(int* array, int size, int query) {int l = 0, r = size - 1;while (l <= r) {int m = l + (r - l) / 2;if (array[m] == query) return m;else if (array[m] < query) l = m + 1;else r = m - 1;}return size;}int ec[114514], *es[114514];
#include <stdio.h>
#include <stdlib.h>
int cmp(const void* x, const void* y) {
int a = *(const int*)x, b = *(const int*)y;
return (a > b) - (a < b);
}
int find(int* array, int size, int query) {
int l = 0, r = size - 1;
while (l <= r) {
int m = l + (r - l) / 2;
if (array[m] == query) return m;
else if (array[m] < query) l = m + 1;
else r = m - 1;
}
return size;
}
int ec[114514], *es[114514];
void ae(int f, int t) {
es[f] = realloc(es[f], sizeof(*es[f]) * (ec[f] + 1));
if (es[f] == NULL) exit(2);
es[f][ec[f]++] = t;
}
int a[114514];
int* memo[114514];
int all_results[114514];
int* each_results[114514];
int luck[114514];
int calc(int node, int from) {
int from_id = find(es[node], ec[node], from);
int ans, i;
if (memo[node][from_id]) return ~memo[node][from_id];
if (luck[node] == 0) {
/* 全ての子について未調査 */
all_results[node] = 0;
luck[node] = -1;
for (i = 0; i < ec[node]; i++) {
if (i != from_id) {
each_results[node][i] = calc(es[node][i], node);
if (each_results[node][i] > 0) all_results[node] += each_results[node][i];
} else {
each_results[node][i] = 0;
luck[node] = i + 1;
}
}
} else if (luck[node] > 0) {
/* 1個の子について未調査 */
each_results[node][luck[node] - 1] = calc(es[node][luck[node] - 1], node);
if (each_results[node][luck[node] - 1] > 0) all_results[node] += each_results[node][luck[node] - 1];
luck[node] = -1;
}
ans = a[node] + all_results[node];
if (from_id < ec[node] && each_results[node][from_id] > 0) ans -= each_results[node][from_id];
return ~(memo[node][from_id] = ~ans);
}
int main(void) {
int n, i;
int ans;
if (scanf("%d", &n) != 1) return 1;
for (i = 1; i <= n; i++) {
int s;
if (scanf("%d%d", &s, &a[i]) != 2) return 1;
if (s != 0) {
ae(s, i);
ae(i, s);
}
}
for (i = 1; i <= n; i++) {
if (ec[i] > 0) {
qsort(es[i], ec[i], sizeof(*es[i]), cmp);
each_results[i] = malloc(sizeof(*each_results[i]) * ec[i]);
}
memo[i] = calloc(ec[i] + 1, sizeof(*memo[i]));
}
ans = calc(1, 0);
for (i = 2; i <= n; i++) {
int candidate = calc(i, 0);
if (candidate > ans) ans = candidate;
}
printf("%d\n", ans);
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | committee - 委員会 (Committee) |
| User | mikecat |
| Language | C23 (GCC 14.2.0) |
| Score | 100 |
| Code Size | 2284 Byte |
| Status | AC |
| Exec Time | 34 ms |
| Memory | 24296 KiB |
Judge Result
| Set Name | Set01 | Set02 | Set03 | Set04 | Set05 | Set06 | Set07 | Set08 | Set09 | Set10 | Set11 | Set12 | Set13 | Set14 | Set15 | Set16 | Set17 | Set18 | Set19 | Set20 | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Score / Max Score | 5 / 5 | 5 / 5 | 5 / 5 | 5 / 5 | 5 / 5 | 5 / 5 | 5 / 5 | 5 / 5 | 5 / 5 | 5 / 5 | 5 / 5 | 5 / 5 | 5 / 5 | 5 / 5 | 5 / 5 | 5 / 5 | 5 / 5 | 5 / 5 | 5 / 5 | 5 / 5 | ||||||||||||||||||||||||||||||||||||||||
| Status |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Set Name | Test Cases |
|---|---|
| Set01 | 01 |
| Set02 | 02 |
| Set03 | 03 |
| Set04 | 04 |
| Set05 | 05 |
| Set06 | 06 |
| Set07 | 07 |
| Set08 | 08 |
| Set09 | 09 |
| Set10 | 10 |
| Set11 | 11 |
| Set12 | 12 |
| Set13 | 13 |
| Set14 | 14 |
| Set15 | 15 |
| Set16 | 16 |
| Set17 | 17 |
| Set18 | 18 |
| Set19 | 19 |
| Set20 | 20 |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 01 | AC | 1 ms | 1864 KiB |
| 02 | AC | 1 ms | 1808 KiB |
| 03 | AC | 1 ms | 1816 KiB |
| 04 | AC | 34 ms | 15108 KiB |
| 05 | AC | 26 ms | 15804 KiB |
| 06 | AC | 1 ms | 1768 KiB |
| 07 | AC | 1 ms | 1684 KiB |
| 08 | AC | 1 ms | 1768 KiB |
| 09 | AC | 34 ms | 15020 KiB |
| 10 | AC | 26 ms | 18068 KiB |
| 11 | AC | 33 ms | 15036 KiB |
| 12 | AC | 33 ms | 15080 KiB |
| 13 | AC | 33 ms | 15000 KiB |
| 14 | AC | 24 ms | 16024 KiB |
| 15 | AC | 22 ms | 14872 KiB |
| 16 | AC | 24 ms | 15976 KiB |
| 17 | AC | 23 ms | 16008 KiB |
| 18 | AC | 27 ms | 24296 KiB |
| 19 | AC | 30 ms | 16304 KiB |
| 20 | AC | 33 ms | 15080 KiB |