結果

問題 No.872 All Tree Path
ユーザー htensaihtensai
提出日時 2020-01-18 19:59:21
言語 Java19
(openjdk 21)
結果
AC  
実行時間 1,190 ms / 3,000 ms
コード長 1,498 bytes
コンパイル時間 2,039 ms
コンパイル使用メモリ 74,816 KB
実行使用メモリ 152,688 KB
最終ジャッジ日時 2023-09-10 10:50:45
合計ジャッジ時間 13,991 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,155 ms
131,768 KB
testcase_01 AC 1,143 ms
132,760 KB
testcase_02 AC 1,154 ms
133,168 KB
testcase_03 AC 757 ms
152,688 KB
testcase_04 AC 42 ms
49,268 KB
testcase_05 AC 1,169 ms
135,092 KB
testcase_06 AC 1,190 ms
133,892 KB
testcase_07 AC 1,145 ms
131,748 KB
testcase_08 AC 221 ms
60,480 KB
testcase_09 AC 220 ms
61,880 KB
testcase_10 AC 217 ms
57,800 KB
testcase_11 AC 224 ms
60,728 KB
testcase_12 AC 210 ms
60,528 KB
testcase_13 AC 42 ms
49,200 KB
testcase_14 AC 43 ms
49,456 KB
testcase_15 AC 43 ms
49,200 KB
testcase_16 AC 44 ms
49,208 KB
testcase_17 AC 43 ms
49,476 KB
testcase_18 AC 43 ms
47,440 KB
testcase_19 AC 44 ms
49,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
    static long[] chiledren;
    static long[] costs;
    static HashMap<Integer, Integer>[] graph;
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        graph = new HashMap[n];
        for (int i = 0; i < n; i++) {
            graph[i] = new HashMap<Integer, Integer>();
        }
        for (int i = 0; i < n - 1; i++) {
            String[] line = br.readLine().split(" ", 3);
            int a = Integer.parseInt(line[0]) - 1;
            int b = Integer.parseInt(line[1]) - 1;
            int c = Integer.parseInt(line[2]);
            graph[a].put(b, c);
            graph[b].put(a, c);
        }
        chiledren = new long[n];
        costs = new long[n];
        getChildrenCount(0, -1);
        long total = 0;
        for (int i = 0; i < n; i++) {
            total += chiledren[i] * (n - chiledren[i]) * costs[i];
        }
        System.out.println(total * 2);
    }
    
    static long getChildrenCount(int to, int from) {
        for (Map.Entry<Integer, Integer> entry : graph[to].entrySet()) {
            int x = entry.getKey();
            if (x == from) {
                costs[to] += entry.getValue();
            } else {
                chiledren[to] += getChildrenCount(x, to);
            }
        }
        chiledren[to]++;
        return chiledren[to];
    }
}
0