結果

問題 No.386 貪欲な領主
ユーザー tentententen
提出日時 2023-04-04 17:55:31
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,530 bytes
コンパイル時間 3,250 ms
コンパイル使用メモリ 91,884 KB
実行使用メモリ 114,872 KB
最終ジャッジ日時 2024-04-08 17:21:07
合計ジャッジ時間 10,908 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 55 ms
51,916 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 55 ms
53,976 KB
testcase_11 AC 54 ms
53,980 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static ArrayList<ArrayList<Integer>> graph = new  ArrayList<>();
    static int[] values;
    static int[] sums;
    static int[][] parents;
    static int[] depths;
    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);
        }
        values = new int[n];
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
        }
        sums = new int[n];
        parents = new int[20][n];
        depths = new int[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]];
            }
        }
        long total = 0;
        int m = sc.nextInt();
        while (m-- > 0) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int c = sc.nextInt();
            int lca = getLCA(a, b);
            long cost = sums[a] + sums[b] - sums[lca] * 2 + values[lca];
            total += cost * c;
        }
        System.out.println(total);
    }
    
    static void setDepth(int idx, int p, int d, int v) {
        parents[0][idx] = p;
        depths[idx] = d;
        sums[idx] = v + values[idx];
        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)) {
                continue;
            }
            a = depths[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();
    }
    
}
0