結果
問題 | No.386 貪欲な領主 |
ユーザー | tenten |
提出日時 | 2023-05-02 14:13:11 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,059 ms / 2,000 ms |
コード長 | 3,506 bytes |
コンパイル時間 | 2,982 ms |
コンパイル使用メモリ | 91,852 KB |
実行使用メモリ | 113,356 KB |
最終ジャッジ日時 | 2024-11-21 05:09:39 |
合計ジャッジ時間 | 10,348 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 56 ms
50,120 KB |
testcase_01 | AC | 57 ms
50,468 KB |
testcase_02 | AC | 57 ms
50,440 KB |
testcase_03 | AC | 56 ms
50,200 KB |
testcase_04 | AC | 1,059 ms
112,752 KB |
testcase_05 | AC | 831 ms
92,668 KB |
testcase_06 | AC | 958 ms
96,452 KB |
testcase_07 | AC | 125 ms
53,160 KB |
testcase_08 | AC | 258 ms
61,208 KB |
testcase_09 | AC | 140 ms
53,440 KB |
testcase_10 | AC | 55 ms
50,496 KB |
testcase_11 | AC | 57 ms
50,104 KB |
testcase_12 | AC | 122 ms
53,340 KB |
testcase_13 | AC | 155 ms
55,476 KB |
testcase_14 | AC | 858 ms
90,464 KB |
testcase_15 | AC | 971 ms
113,356 KB |
ソースコード
import java.io.*; import java.util.*; import java.util.stream.*; public class Main { static ArrayList<ArrayList<Integer>> graph = new ArrayList<>(); static int[][] parents; static int[] depths; static int[] costs; static long[] sums; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); for (int i = 0; i < n; i++) { graph.add(new ArrayList<>()); } for (int i = 0; i < n - 1; i++) { int a = sc.nextInt(); int b = sc.nextInt(); graph.get(a).add(b); graph.get(b).add(a); } costs = new int[n]; for (int i = 0; i < n; i++) { costs[i] = sc.nextInt(); } parents = new int[20][n]; depths = new int[n]; sums = new long[n]; setDepth(0, 0, 0, 0); for (int i = 1; i < 20; i++) { for (int j = 0; j < n; j++) { parents[i][j] = parents[i - 1][parents[i - 1][j]]; } } int m = sc.nextInt(); long ans = 0; while (m-- > 0) { int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); int lca = getLCA(a, b); long total = sums[a] + sums[b] - sums[lca] * 2 + costs[lca]; ans += total * c; } System.out.println(ans); } static void setDepth(int idx, int p, int d, long v) { parents[0][idx] = p; depths[idx] = d; sums[idx] = costs[idx] + v; for (int x : graph.get(idx)) { if (x != p) { setDepth(x, idx, d + 1, sums[idx]); } } } static int getLCA(int a, int b) { if (depths[a] < depths[b]) { return getLCA(b, a); } for (int i = 19; i >= 0; i--) { if (depths[a] - depths[b] >= (1 << i)) { a = parents[i][a]; } } if (a == b) { return a; } for (int i = 19; i >= 0; i--) { if (parents[i][a] != parents[i][b]) { a = parents[i][a]; b = parents[i][b]; } } return parents[0][a]; } } class Utilities { static String arrayToLineString(Object[] arr) { return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n")); } static String arrayToLineString(int[] arr) { return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new)); } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public int[] nextIntArray() throws Exception { return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }