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 300000
#define INF 0x3f3f3f3f3f3f3f3fLL
long long min(long long a, long long b) { return a < b ? a : b; }
long long max(long long a, long long b) { return a > b ? a : b; }
int *ej[N], *ew[N], eo[N];
void append(int i, int j, int w) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0) {
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ew[i] = (int *) realloc(ew[i], o * 2 * sizeof *ew[i]);
}
ej[i][o] = j, ew[i][o] = w;
}
long long dp[N], dq[N], d; int qu[N], cnt;
void dfs(int p, int i) {
int o;
dp[i] = -1, dq[i] = INF;
for (o = eo[i]; o--; ) {
int j = ej[i][o], w = ew[i][o];
if (j != p) {
dfs(i, j);
if (dp[j] != -1) {
if (dp[j] + w > d)
qu[cnt++] = j, dq[j] = 0;
else
dp[i] = max(dp[i], dp[j] + w);
}
dq[i] = min(dq[i], dq[j] + w);
}
}
if (dp[i] != -1 && dp[i] + dq[i] <= d)
dp[i] = -1;
if (dq[i] > d)
dp[i] = max(dp[i], 0);
}
int main() {
int n, h, i, j, w;
scanf("%d%lld", &n, &d);
for (i = 0; i < n; i++) {
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
ew[i] = (int *) malloc(2 * sizeof *ew[i]);
}
for (h = 0; h < n - 1; h++) {
scanf("%d%d%d", &i, &j, &w), i--, j--;
append(i, j, w), append(j, i, w);
}
dfs(-1, 0);
if (dp[0] != -1)
qu[cnt++] = 0;
printf("%d\n", cnt);
while (cnt--)
printf("%d ", qu[cnt] + 1);
printf("\n");
return 0;
}
Compilation message (stderr)
Firefighting.c: In function 'append':
Firefighting.c:15:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
15 | if (o >= 2 && (o & o - 1) == 0) {
| ~~^~~
Firefighting.c: In function 'main':
Firefighting.c:51:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | scanf("%d%lld", &n, &d);
| ^~~~~~~~~~~~~~~~~~~~~~~
Firefighting.c:57:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | scanf("%d%d%d", &i, &j, &w), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |