結果

問題 No.386 貪欲な領主
ユーザー htensaihtensai
提出日時 2020-06-12 14:09:37
言語 Java19
(openjdk 21)
結果
AC  
実行時間 1,009 ms / 2,000 ms
コード長 2,323 bytes
コンパイル時間 2,096 ms
コンパイル使用メモリ 74,992 KB
実行使用メモリ 110,160 KB
最終ジャッジ日時 2023-09-06 09:10:53
合計ジャッジ時間 9,675 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
49,312 KB
testcase_01 AC 44 ms
49,532 KB
testcase_02 AC 47 ms
49,392 KB
testcase_03 AC 46 ms
49,268 KB
testcase_04 AC 1,009 ms
107,768 KB
testcase_05 AC 818 ms
87,352 KB
testcase_06 AC 874 ms
85,780 KB
testcase_07 AC 108 ms
54,320 KB
testcase_08 AC 241 ms
58,632 KB
testcase_09 AC 127 ms
56,192 KB
testcase_10 AC 45 ms
49,196 KB
testcase_11 AC 45 ms
49,316 KB
testcase_12 AC 106 ms
53,924 KB
testcase_13 AC 150 ms
55,040 KB
testcase_14 AC 876 ms
88,196 KB
testcase_15 AC 869 ms
110,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
	static ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
	static int[] costs;
	static int[] depth;
	static long[] costfroms;
	static int[][] parents;
	public static void main (String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		for (int i = 0; i < n; i++) {
		    graph.add(new ArrayList<>());
		}
		for (int i = 0; i < n - 1; i++) {
		    String[] line = br.readLine().split(" ", 2);
		    int a = Integer.parseInt(line[0]);
		    int b = Integer.parseInt(line[1]);
		    graph.get(a).add(b);
		    graph.get(b).add(a);
		}
		costs = new int[n];
		for (int i = 0; i < n; i++) {
		    costs[i] = Integer.parseInt(br.readLine());
		}
		parents = new int[18][n];
		costfroms = new long[n];
		depth = new int[n];
		setParent(0, 0, 0, 0);
		for (int i = 1; i < 18; i++) {
		    for (int j = 0; j < n; j++) {
		        parents[i][j] = parents[i - 1][parents[i - 1][j]];
		    }
		}
		int m = Integer.parseInt(br.readLine());
		long total = 0;
		for (int i = 0; i < m; i++) {
		    String[] line = br.readLine().split(" ", 3);
		    int a = Integer.parseInt(line[0]);
		    int b = Integer.parseInt(line[1]);
		    int c = Integer.parseInt(line[2]);
		    int lca = getLCA(a, b);
		    long price = costfroms[a] + costfroms[b] - costfroms[lca] * 2 + costs[lca];
		    total += price * c;
		}
		System.out.println(total);
	}
	
	static void setParent(int idx, int parent, long value, int d) {
	    parents[0][idx] = parent;
	    costfroms[idx] = value + costs[idx];
	    depth[idx] = d;
	    for (int x : graph.get(idx)) {
	        if (x == parent) {
	            continue;
	        }
	        setParent(x, idx, costfroms[idx], d + 1);
	    }
	}
	
	static int getLCA(int a, int b) {
	    if (depth[a] > depth[b]) {
	        return getLCA(b, a);
	    }
	    for (int i = 17; i >= 0; i--) {
	        if (depth[b] - depth[a] >= (1 << i)) {
	            b = parents[i][b];
	        }
	    }
	    for (int i = 17; i >= 0; i--) {
	        if (parents[i][a] != parents[i][b]) {
	            a = parents[i][a];
	            b = parents[i][b];
	        }
	    }
	    if (a != b) {
            a = parents[0][a];
            b = parents[0][b];
	    }
	    return a;
	}
}
0