結果
| 問題 |
No.872 All Tree Path
|
| コンテスト | |
| ユーザー |
htensai
|
| 提出日時 | 2020-01-18 19:59:21 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 1,306 ms / 3,000 ms |
| コード長 | 1,498 bytes |
| コンパイル時間 | 2,280 ms |
| コンパイル使用メモリ | 78,580 KB |
| 実行使用メモリ | 139,200 KB |
| 最終ジャッジ日時 | 2024-06-28 02:13:43 |
| 合計ジャッジ時間 | 15,346 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 18 |
ソースコード
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];
}
}
htensai