結果

問題 No.872 All Tree Path
ユーザー tenten
提出日時 2020-08-31 01:12:27
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 1,008 bytes
コンパイル時間 3,441 ms
コンパイル使用メモリ 79,492 KB
実行使用メモリ 150,072 KB
最終ジャッジ日時 2024-11-15 16:09:06
合計ジャッジ時間 30,056 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 6 WA * 12
権限があれば一括ダウンロードができます

ソースコード

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