結果

問題 No.872 All Tree Path
ユーザー bal4ubal4u
提出日時 2019-09-06 14:31:30
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 93 ms / 3,000 ms
コード長 1,210 bytes
コンパイル時間 1,207 ms
コンパイル使用メモリ 29,776 KB
実行使用メモリ 36,152 KB
最終ジャッジ日時 2023-09-06 02:53:44
合計ジャッジ時間 2,737 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
20,608 KB
testcase_01 AC 93 ms
20,516 KB
testcase_02 AC 92 ms
20,576 KB
testcase_03 AC 52 ms
36,152 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 87 ms
20,592 KB
testcase_06 AC 93 ms
20,568 KB
testcase_07 AC 86 ms
20,568 KB
testcase_08 AC 5 ms
4,380 KB
testcase_09 AC 6 ms
4,380 KB
testcase_10 AC 5 ms
4,376 KB
testcase_11 AC 5 ms
4,380 KB
testcase_12 AC 6 ms
4,376 KB
testcase_13 AC 0 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 0 ms
4,376 KB
testcase_16 AC 0 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 0 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: 関数 ‘in’ 内:
main.c:12:14: 警告: 関数 ‘getchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
   12 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:18:24: 備考: in expansion of macro ‘gc’
   18 |         int n = 0, c = gc();
      |                        ^~

ソースコード

diff #

// yukicoder: 872 All Tree Path
// 2019.9.6 bal4u

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef long long ll;

//// 高速入力
#if 1
#define gc() getchar_unlocked()
#else
#define gc() getchar()
#endif

int in() {   // 非負整数の入力
	int n = 0, c = gc();
	do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0');
	return n;
}

int N;
int hi[200005], *to[200005], *w[200005];
ll ans;

int dfs(int now, int par) {
	int i, j, s, f;
	
	s = 1;
	for (i = 0; i < hi[now]; i++) if ((j = to[now][i]) != par) {
		s += (f = dfs(j, now));
		ans += ((ll)w[now][i] * f * (N-f)) << 1;
	}
	return s;
}
				
int main()
{
	int i, k, u, v;
	int *memo, sz;
	
	N = in();
	memo = malloc(N*sizeof(int)*3);
	sz = 0; for (i = 1; i < N; i++) {
		u = in()-1, v = in()-1;
		memo[sz++] = u, memo[sz++] = v, memo[sz++] = in();
		hi[u]++, hi[v]++;
	}
	for (i = 0; i < N; i++) if (hi[i]) {
		to[i] = malloc(hi[i] * sizeof(int));
		w[i] = malloc(hi[i] * sizeof(int));
	}
	memset(hi, 0, sizeof(int)*N);
	
	i = 0; while (i < sz) {
		u = memo[i++], v = memo[i++];
		k = hi[u]++, to[u][k] = v, w[u][k] = memo[i];
		k = hi[v]++, to[v][k] = u, w[v][k] = memo[i++];
	}
	
	dfs(0, -1);
	printf("%lld\n", ans);
	return 0;
}
0