結果

問題 No.872 All Tree Path
ユーザー tentententen
提出日時 2020-08-31 01:12:27
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,008 bytes
コンパイル時間 2,556 ms
コンパイル使用メモリ 82,172 KB
実行使用メモリ 150,196 KB
最終ジャッジ日時 2024-04-27 13:51:02
合計ジャッジ時間 28,308 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 131 ms
54,224 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 132 ms
53,780 KB
testcase_14 AC 136 ms
54,352 KB
testcase_15 AC 146 ms
54,276 KB
testcase_16 AC 144 ms
53,932 KB
testcase_17 AC 141 ms
53,988 KB
testcase_18 AC 142 ms
54,160 KB
testcase_19 AC 141 ms
54,192 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) * (n - sum) * sum * 2;
        return sum;
    }
}
0