提出 #52100242


ソースコード 拡げる

Copy
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
struct edge_s {
int to, id, rev_id;
};
int ec[114514];
struct edge_s *es[114514];
void ae(int f, int t, int id, int rev_id) {
es[f] = realloc(es[f], sizeof(*es[f]) * (ec[f] + 1));
if (es[f] == NULL) exit(2);
es[f][ec[f]].to = t;
es[f][ec[f]].id = id;
es[f][ec[f]].rev_id = rev_id;
ec[f]++;
}
int N;
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>

struct edge_s {
	int to, id, rev_id;
};

int ec[114514];
struct edge_s *es[114514];

void ae(int f, int t, int id, int rev_id) {
	es[f] = realloc(es[f], sizeof(*es[f]) * (ec[f] + 1));
	if (es[f] == NULL) exit(2);
	es[f][ec[f]].to = t;
	es[f][ec[f]].id = id;
	es[f][ec[f]].rev_id = rev_id;
	ec[f]++;
}

int N;
int A[114514], B[114514];
int C[114514];

struct meow_s {
	uint64_t f_value;
	uint64_t sum_of_C;
};

/* 辺 (両方向) + 最初のノード (来た辺なし) */
struct meow_s memo[114514 * 3];
char memo_valid[114514 * 3];

struct meow_s calc(int node, int from_edge_id) {
	int memo_id = from_edge_id < 0 ? 2 * N + node : from_edge_id;
	struct meow_s ret = {0, 0};
	int i;
	if (memo_valid[memo_id]) return memo[memo_id];
	for (i = 0; i < ec[node]; i++) {
		if (es[node][i].rev_id != from_edge_id) {
			struct meow_s delta = calc(es[node][i].to, es[node][i].id);
			/* 下層の分を足し、さらに上がったので C_i の合計を足す */
			ret.f_value += delta.f_value + delta.sum_of_C;
			/* 新しい C_i の合計を求める */
			ret.sum_of_C += delta.sum_of_C;
		}
	}
	ret.sum_of_C += C[node];
	memo[memo_id] = ret;
	memo_valid[memo_id] = 1;
	return ret;
}

int main(void) {
	int i;
	uint64_t ans;
	if (scanf("%d", &N) != 1) return 1;
	for (i = 1; i < N; i++) {
		if (scanf("%d%d", &A[i], &B[i]) != 2) return 1;
		ae(A[i], B[i], 2 * (i - 1), 2 * (i - 1) + 1);
		ae(B[i], A[i], 2 * (i - 1) + 1, 2 * (i - 1));
	}
	for (i = 1; i <= N; i++) {
		if (scanf("%d", &C[i]) != 1) return 1;
	}
	ans = calc(1, -1).f_value;
	for (i = 2; i <= N; i++) {
		uint64_t candidate = calc(i, -1).f_value;
		if (candidate < ans) ans = candidate;
	}
	printf("%" PRIu64 "\n", ans);
	return 0;
}

/*

f(x) = (C_i × そこに行くまでのステップ数) の合計
これをサブツリーについて計算する
単純に C_i の和 → これを1段深くなるごとに加える

*/

提出情報

提出日時
問題 E - Minimize Sum of Distances
ユーザ mikecat
言語 C (gcc 12.2.0)
得点 0
コード長 2030 Byte
結果 TLE
実行時間 2211 ms
メモリ 19700 KB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 0 / 475
結果
AC × 3
AC × 23
TLE × 8
セット名 テストケース
Sample example0.txt, example1.txt, example2.txt
All example0.txt, example1.txt, example2.txt, test_00.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt, test_22.txt, test_23.txt, test_24.txt, test_25.txt, test_26.txt, test_27.txt
ケース名 結果 実行時間 メモリ
example0.txt AC 0 ms 1648 KB
example1.txt AC 1 ms 1744 KB
example2.txt AC 1 ms 1648 KB
test_00.txt AC 32 ms 8640 KB
test_01.txt AC 20 ms 6312 KB
test_02.txt AC 39 ms 9868 KB
test_03.txt AC 1127 ms 3912 KB
test_04.txt TLE 2210 ms 9012 KB
test_05.txt TLE 2207 ms 6232 KB
test_06.txt AC 23 ms 8688 KB
test_07.txt AC 21 ms 9392 KB
test_08.txt AC 17 ms 6880 KB
test_09.txt AC 55 ms 12732 KB
test_10.txt AC 50 ms 12752 KB
test_11.txt AC 55 ms 12680 KB
test_12.txt AC 48 ms 12844 KB
test_13.txt AC 56 ms 12836 KB
test_14.txt AC 48 ms 12844 KB
test_15.txt TLE 2207 ms 11636 KB
test_16.txt TLE 2211 ms 11556 KB
test_17.txt TLE 2208 ms 11652 KB
test_18.txt TLE 2208 ms 11644 KB
test_19.txt TLE 2211 ms 11576 KB
test_20.txt TLE 2208 ms 11716 KB
test_21.txt AC 66 ms 19448 KB
test_22.txt AC 58 ms 19700 KB
test_23.txt AC 68 ms 17724 KB
test_24.txt AC 57 ms 16976 KB
test_25.txt AC 69 ms 18204 KB
test_26.txt AC 60 ms 18600 KB
test_27.txt AC 0 ms 1756 KB


2024-04-07 (日)
22:10:19 +00:00