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
long long max(long long a, long long b) { return a > b ? a : b; }
int *ej[N], eo[N], ww[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;
}
long long dp[N];
void dfs(int p, int i) {
int o;
dp[i] = 0;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p) {
dfs(i, j);
dp[i] = max(dp[i], dp[j]);
}
}
dp[i] += ww[i];
}
int main() {
static int qu[N];
int n, k, cnt, h, i, j, p, o;
scanf("%d%d", &n, &k);
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);
}
for (i = 0; i < n; i++)
scanf("%d", &ww[i]);
if (k == 1) {
dfs(-1, 0);
p = -1, i = 0, cnt = 0;
while (1) {
qu[cnt++] = i;
for (o = eo[i]; o--; ) {
j = ej[i][o];
if (j != p && dp[i] == dp[j] + ww[i]) {
i = j;
break;
}
}
if (o < 0)
break;
}
printf("%lld\n", dp[0]);
printf("%d\n", cnt);
for (h = 0; h < cnt; h++)
printf("%d ", qu[h] + 1);
printf("\n");
}
return 0;
}
Compilation message (stderr)
Main.c: In function 'append':
Main.c:13:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
13 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
Main.c: In function 'main':
Main.c:39:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
39 | scanf("%d%d", &n, &k);
| ^~~~~~~~~~~~~~~~~~~~~
Main.c:43:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
Main.c:47:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | scanf("%d", &ww[i]);
| ^~~~~~~~~~~~~~~~~~~
# | 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... |