結果

問題 No.872 All Tree Path
ユーザー tentententen
提出日時 2020-08-31 01:14:39
言語 Java21
(openjdk 21)
結果
AC  
実行時間 2,855 ms / 3,000 ms
コード長 1,014 bytes
コンパイル時間 2,211 ms
コンパイル使用メモリ 79,372 KB
実行使用メモリ 145,336 KB
最終ジャッジ日時 2024-04-27 13:51:58
合計ジャッジ時間 27,882 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,447 ms
133,440 KB
testcase_01 AC 2,356 ms
132,880 KB
testcase_02 AC 2,511 ms
133,804 KB
testcase_03 AC 2,855 ms
145,336 KB
testcase_04 AC 135 ms
54,060 KB
testcase_05 AC 2,370 ms
136,372 KB
testcase_06 AC 2,398 ms
133,748 KB
testcase_07 AC 2,418 ms
135,388 KB
testcase_08 AC 577 ms
63,632 KB
testcase_09 AC 597 ms
63,668 KB
testcase_10 AC 622 ms
63,784 KB
testcase_11 AC 626 ms
63,832 KB
testcase_12 AC 614 ms
63,752 KB
testcase_13 AC 132 ms
54,228 KB
testcase_14 AC 132 ms
53,868 KB
testcase_15 AC 140 ms
54,180 KB
testcase_16 AC 142 ms
54,412 KB
testcase_17 AC 142 ms
53,964 KB
testcase_18 AC 143 ms
54,144 KB
testcase_19 AC 143 ms
53,964 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static long total = 0;
    static int n;
    static ArrayList<HashMap<Integer, Integer>> graph;
	public static void main (String[] args) {
    	Scanner sc = new Scanner(System.in);
    	n = sc.nextInt();
    	graph = new ArrayList<>();
    	for (int i = 0; i < n; i++) {
    	    graph.add(new HashMap<>());
    	}
    	for (int i = 0; i < n - 1; i++) {
    	    int a = sc.nextInt() - 1;
    	    int b = sc.nextInt() - 1;
    	    int c = sc.nextInt();
    	    graph.get(a).put(b, c);
    	    graph.get(b).put(a, c);
    	}
    	for (int x : graph.get(0).keySet()) {
    	    calc(x, 0);
    	}
    	System.out.println(total);
    }
    
    static int calc(int idx, int parent) {
        int sum = 1;
        for (int x : graph.get(idx).keySet()) {
            if (x == parent) {
                continue;
            }
            sum += calc(x, idx);
        }
        total += graph.get(idx).get(parent) * (long)(n - sum) * sum * 2;
        return sum;
    }
}
0