結果
問題 | No.806 木を道に |
ユーザー | htensai |
提出日時 | 2020-06-10 13:49:35 |
言語 | Java21 (openjdk 21) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,579 bytes |
コンパイル時間 | 4,924 ms |
コンパイル使用メモリ | 76,164 KB |
実行使用メモリ | 85,140 KB |
最終ジャッジ日時 | 2023-09-05 07:37:18 |
合計ジャッジ時間 | 24,506 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge14 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 128 ms
56,004 KB |
testcase_01 | AC | 126 ms
56,008 KB |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | AC | 132 ms
55,904 KB |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | AC | 129 ms
55,792 KB |
testcase_08 | AC | 129 ms
55,804 KB |
testcase_09 | AC | 128 ms
53,572 KB |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | AC | 676 ms
68,732 KB |
testcase_25 | AC | 822 ms
77,192 KB |
testcase_26 | AC | 570 ms
65,344 KB |
testcase_27 | AC | 1,060 ms
85,140 KB |
testcase_28 | AC | 263 ms
59,444 KB |
ソースコード
import java.util.*; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); ArrayList<ArrayList<Integer>> graph = new ArrayList<>(); for (int i = 0; i < n; i++) { graph.add(new ArrayList<>()); } for (int i = 0; i < n - 1; i++) { int a = sc.nextInt() - 1; int b = sc.nextInt() - 1; graph.get(a).add(b); graph.get(b).add(a); } PriorityQueue<Path> queue = new PriorityQueue<>(); int[] costs = new int[n]; queue.add(new Path(0, 0)); search(graph, queue, costs); int max = 0; int maxIdx = 0; for (int i = 0; i < n; i++) { if (max < costs[i]) { max = costs[i]; maxIdx = i; } } queue.add(new Path(maxIdx, 0)); search(graph, queue, costs); Arrays.sort(costs); System.out.println(n - costs[n - 1] - 1); } static void search(ArrayList<ArrayList<Integer>> graph, PriorityQueue<Path> queue, int[] costs) { Arrays.fill(costs, Integer.MAX_VALUE); while (queue.size() > 0) { Path p = queue.poll(); if (costs[p.idx] <= p.value) { continue; } costs[p.idx] = p.value; for (int x : graph.get(p.idx)) { queue.add(new Path(x, p.value + 1)); } } } static class Path implements Comparable<Path> { int idx; int value; public Path(int idx, int value) { this.idx = idx; this.value = value; } public int compareTo(Path another) { return value - another.value; } } }