답안 #823905

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
823905 2023-08-13T09:58:11 Z rainboy Travelling Trader (CCO23_day2problem2) C
2 / 25
114 ms 30424 KB
#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

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]);
      |   ^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 75 ms 13340 KB Output is correct
4 Correct 80 ms 13336 KB Output is correct
5 Correct 114 ms 12896 KB Output is correct
6 Correct 78 ms 13976 KB Output is correct
7 Correct 66 ms 13632 KB Output is correct
8 Correct 72 ms 13200 KB Output is correct
9 Correct 113 ms 30424 KB Output is correct
10 Correct 98 ms 21576 KB Output is correct
11 Correct 58 ms 12744 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Incorrect 0 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Incorrect 0 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Incorrect 0 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 340 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 340 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -