結果

問題 No.872 All Tree Path
ユーザー nebukuro09
提出日時 2019-08-31 00:30:49
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 310 ms / 3,000 ms
コード長 837 bytes
コンパイル時間 845 ms
コンパイル使用メモリ 119,464 KB
実行使用メモリ 47,676 KB
最終ジャッジ日時 2024-06-22 02:26:58
合計ジャッジ時間 4,269 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

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