結果

問題 No.872 All Tree Path
ユーザー ks2mks2m
提出日時 2019-08-30 22:16:20
言語 Java21
(openjdk 21)
結果
AC  
実行時間 899 ms / 3,000 ms
コード長 1,269 bytes
コンパイル時間 2,830 ms
コンパイル使用メモリ 74,568 KB
実行使用メモリ 114,364 KB
最終ジャッジ日時 2023-08-14 05:39:21
合計ジャッジ時間 11,316 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 878 ms
92,044 KB
testcase_01 AC 876 ms
91,944 KB
testcase_02 AC 793 ms
90,112 KB
testcase_03 AC 604 ms
114,364 KB
testcase_04 AC 45 ms
49,188 KB
testcase_05 AC 899 ms
92,016 KB
testcase_06 AC 894 ms
92,160 KB
testcase_07 AC 785 ms
90,264 KB
testcase_08 AC 185 ms
55,676 KB
testcase_09 AC 180 ms
56,372 KB
testcase_10 AC 171 ms
53,924 KB
testcase_11 AC 184 ms
55,944 KB
testcase_12 AC 171 ms
55,644 KB
testcase_13 AC 43 ms
49,308 KB
testcase_14 AC 43 ms
49,712 KB
testcase_15 AC 44 ms
49,544 KB
testcase_16 AC 43 ms
49,192 KB
testcase_17 AC 44 ms
49,196 KB
testcase_18 AC 45 ms
49,324 KB
testcase_19 AC 45 ms
49,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main {
	static Hen[] arr;
	static List<List<Hen>> list;

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		arr = new Hen[n - 1];
		list = new ArrayList<>(n);
		for (int i = 0; i < n; i++) {
			list.add(new ArrayList<>());
		}
		for (int i = 0; i < n - 1; i++) {
			String[] sa = br.readLine().split(" ");
			Hen h = new Hen();
			h.i = i;
			h.u = Integer.parseInt(sa[0]) - 1;
			h.v = Integer.parseInt(sa[1]) - 1;
			h.w = Integer.parseInt(sa[2]);
			arr[i] = h;
			list.get(h.u).add(h);
			list.get(h.v).add(h);
		}
		br.close();

		dfs(0, -1);

		long ans = 0;
		for (int i = 0; i < arr.length; i++) {
			ans += (long) arr[i].w * arr[i].c * (n - arr[i].c) * 2;
		}
		System.out.println(ans);
	}

	static class Hen {
		int i, u, v, w, c;
	}

	static int dfs(int x, int p) {
		List<Hen> nexts = list.get(x);
		int sum = 1;
		for (Hen h : nexts) {
			int next = h.u;
			if (next == x) {
				next = h.v;
			}
			if (next != p) {
				int c = dfs(next, x);
				h.c = c;
				sum += c;
			}
		}
		return sum;
	}
}
0