結果

問題 No.872 All Tree Path
ユーザー nebukuro09nebukuro09
提出日時 2019-08-31 00:30:49
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 358 ms / 3,000 ms
コード長 837 bytes
コンパイル時間 614 ms
コンパイル使用メモリ 104,512 KB
実行使用メモリ 48,232 KB
最終ジャッジ日時 2023-09-04 02:38:00
合計ジャッジ時間 4,677 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 358 ms
39,916 KB
testcase_01 AC 344 ms
39,908 KB
testcase_02 AC 344 ms
39,916 KB
testcase_03 AC 274 ms
48,232 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 341 ms
40,120 KB
testcase_06 AC 343 ms
39,912 KB
testcase_07 AC 342 ms
39,924 KB
testcase_08 AC 29 ms
7,192 KB
testcase_09 AC 30 ms
8,464 KB
testcase_10 AC 30 ms
8,000 KB
testcase_11 AC 30 ms
8,008 KB
testcase_12 AC 29 ms
7,220 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;

void main() {
    auto N = readln.chomp.to!int;
    auto g = new Tuple!(int, long)[][](N);
    foreach (_; 0..N-1) {
        auto s = readln.split.map!(to!int);
        g[s[0]-1] ~= tuple(s[1]-1, s[2].to!long);
        g[s[1]-1] ~= tuple(s[0]-1, s[2].to!long);
    }

    auto sub = new long[](N);
    long ans = 0;

    void dfs(int n, int p) {
        sub[n] = 1;
        foreach (to; g[n]) {
            int m = to[0];
            long w = to[1];
            if (m == p) continue;
            dfs(m, n);
            ans += sub[m] * (N - sub[m]) * w;
            sub[n] += sub[m];
        }
    }

    dfs(0, -1);
    writeln(ans * 2);
}
0