結果
問題 | No.901 K-ary εxtrεεmε |
ユーザー | tenten |
提出日時 | 2024-03-12 13:01:34 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,332 ms / 3,000 ms |
コード長 | 3,502 bytes |
コンパイル時間 | 2,879 ms |
コンパイル使用メモリ | 85,296 KB |
実行使用メモリ | 118,996 KB |
最終ジャッジ日時 | 2024-09-29 22:17:02 |
合計ジャッジ時間 | 31,757 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 712 ms
106,660 KB |
testcase_01 | AC | 69 ms
38,200 KB |
testcase_02 | AC | 124 ms
41,180 KB |
testcase_03 | AC | 123 ms
41,684 KB |
testcase_04 | AC | 129 ms
41,140 KB |
testcase_05 | AC | 127 ms
41,396 KB |
testcase_06 | AC | 133 ms
41,300 KB |
testcase_07 | AC | 1,012 ms
105,332 KB |
testcase_08 | AC | 1,005 ms
101,568 KB |
testcase_09 | AC | 986 ms
98,496 KB |
testcase_10 | AC | 993 ms
101,632 KB |
testcase_11 | AC | 1,049 ms
107,100 KB |
testcase_12 | AC | 1,008 ms
97,556 KB |
testcase_13 | AC | 1,008 ms
96,724 KB |
testcase_14 | AC | 1,000 ms
97,072 KB |
testcase_15 | AC | 995 ms
97,576 KB |
testcase_16 | AC | 1,001 ms
96,948 KB |
testcase_17 | AC | 1,165 ms
107,700 KB |
testcase_18 | AC | 1,145 ms
107,740 KB |
testcase_19 | AC | 1,120 ms
118,996 KB |
testcase_20 | AC | 1,134 ms
113,328 KB |
testcase_21 | AC | 1,111 ms
115,436 KB |
testcase_22 | AC | 1,032 ms
96,776 KB |
testcase_23 | AC | 998 ms
96,636 KB |
testcase_24 | AC | 1,003 ms
96,696 KB |
testcase_25 | AC | 1,011 ms
96,600 KB |
testcase_26 | AC | 1,023 ms
97,048 KB |
testcase_27 | AC | 1,332 ms
113,764 KB |
testcase_28 | AC | 1,147 ms
113,936 KB |
testcase_29 | AC | 1,149 ms
113,904 KB |
ソースコード
import java.io.*; import java.util.*; import java.util.function.*; import java.util.stream.*; public class Main { static List<Map<Integer, Integer>> graph = new ArrayList<>(); static int[][] parents; static int[] depths; static long[] weights; static int[] dfs; static int current = 0; 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 HashMap<>()); } for (int i = 0; i < n - 1; i++) { int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); graph.get(a).put(b, c); graph.get(b).put(a, c); } parents = new int[20][n]; depths = new int[n]; weights = new long[n]; dfs = 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]]; } } int q = sc.nextInt(); String result = IntStream.range(0, q).mapToObj(p -> { int k = sc.nextInt(); List<Integer> list = IntStream.range(0, k) .map(i -> sc.nextInt()).boxed().sorted((a, b) -> dfs[a] - dfs[b]).toList(); long ans = 0; for (int i = 0; i < k; i++) { ans += getLength(list.get(i), list.get((i + 1) % k)); } return String.valueOf(ans / 2); }).collect(Collectors.joining("\n")); System.out.println(result); } static void setDepth(int idx, int p, int d, long v) { parents[0][idx] = p; depths[idx] = d; weights[idx] = v; dfs[idx] = current++; for (int x : graph.get(idx).keySet()) { if (x != p) { setDepth(x, idx, d + 1, v + graph.get(idx).get(x)); } } } static long getLength(int a, int b) { return weights[a] + weights[b] - weights[getLCA(a, b)] * 2; } 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 Scanner { BufferedReader br; StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); } catch (Exception e) { } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { try { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } } catch (Exception e) { e.printStackTrace(); } finally { return st.nextToken(); } } }