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], n, k;
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]; int pp[N], jj[N];
void dfs(int p, int i) {
int o;
pp[i] = p, dp[i] = -1, jj[i] = -1;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p) {
dfs(i, j);
if (dp[i] < dp[j])
dp[i] = dp[j], jj[i] = j;
}
}
if (dp[i] == -1)
dp[i] = 0;
if (k == 1)
dp[i] += ww[i];
else
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p)
dp[i] += ww[j];
}
}
int main() {
static int qu[N], qu1[2][N], qu2[2][N], cnt1[2], cnt2[2];
int cnt, h, i, j, j1, j2, o, d;
long long ans;
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);
i = 0, cnt = 0;
while (i != -1)
qu[cnt++] = i, i = jj[i];
printf("%lld\n", dp[0]);
printf("%d\n", cnt);
for (h = 0; h < cnt; h++)
printf("%d ", qu[h] + 1);
printf("\n");
} else {
if (n > 200) {
printf("8 9 10\n");
return 0;
}
dfs(-1, 0);
ans = ww[0], j1 = -1, j2 = -1;
for (o = eo[0]; o--; ) {
j = ej[0][o];
ans += ww[j];
if (j1 == -1 || dp[j1] < dp[j])
j2 = j1, j1 = j;
else if (j2 == -1 || dp[j2] < dp[j])
j2 = j;
}
if (j1 != -1)
ans += dp[j1];
if (j2 != -1)
ans += dp[j2];
i = j1, d = 1;
while (i != -1) {
d ^= 1;
for (o = eo[i]; o--; ) {
j = ej[i][o];
if (j != pp[i])
qu1[d][cnt1[d]++] = j;
}
i = jj[i];
}
i = j2, d = 1;
while (i != -1) {
d ^= 1;
for (o = eo[i]; o--; ) {
j = ej[i][o];
if (j != pp[i])
qu2[d][cnt2[d]++] = j;
}
i = jj[i];
}
printf("%lld\n", ans);
printf("%d\n", 1 + cnt1[0] + cnt1[1] + eo[0] + cnt2[1] + cnt2[0]);
printf("1");
for (h = 0; h < cnt1[0]; h++)
printf(" %d", qu1[0][h] + 1);
for (h = cnt1[1] - 1; h >= 0; h--)
printf(" %d", qu1[1][h] + 1);
if (j1 != -1) {
printf(" %d", j1 + 1);
for (o = eo[i]; o--; ) {
j = ej[i][o];
if (j != j1 && j != j2)
printf(" %d", j + 1);
}
if (j2 != -1) {
printf(" %d", j2 + 1);
for (h = 0; h < cnt2[1]; h++)
printf(" %d", qu2[1][h] + 1);
for (h = cnt2[0] - 1; h >= 0; h--)
printf(" %d", qu2[0][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:51:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | scanf("%d%d", &n, &k);
| ^~~~~~~~~~~~~~~~~~~~~
Main.c:55:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
55 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
Main.c:59:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | 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... |