結果
問題 | No.1582 Vertexes vs Edges |
ユーザー | tenten |
提出日時 | 2023-02-03 12:56:10 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 579 ms / 2,000 ms |
コード長 | 2,834 bytes |
コンパイル時間 | 2,915 ms |
コンパイル使用メモリ | 92,344 KB |
実行使用メモリ | 66,648 KB |
最終ジャッジ日時 | 2024-07-02 13:59:56 |
合計ジャッジ時間 | 13,566 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 52 ms
37,148 KB |
testcase_01 | AC | 52 ms
36,784 KB |
testcase_02 | AC | 51 ms
36,692 KB |
testcase_03 | AC | 54 ms
37,332 KB |
testcase_04 | AC | 128 ms
40,444 KB |
testcase_05 | AC | 351 ms
60,940 KB |
testcase_06 | AC | 132 ms
40,784 KB |
testcase_07 | AC | 156 ms
45,168 KB |
testcase_08 | AC | 241 ms
50,244 KB |
testcase_09 | AC | 335 ms
59,976 KB |
testcase_10 | AC | 252 ms
52,052 KB |
testcase_11 | AC | 138 ms
41,764 KB |
testcase_12 | AC | 217 ms
48,136 KB |
testcase_13 | AC | 292 ms
53,112 KB |
testcase_14 | AC | 305 ms
53,948 KB |
testcase_15 | AC | 372 ms
57,060 KB |
testcase_16 | AC | 255 ms
49,316 KB |
testcase_17 | AC | 291 ms
53,696 KB |
testcase_18 | AC | 402 ms
61,672 KB |
testcase_19 | AC | 281 ms
53,476 KB |
testcase_20 | AC | 292 ms
53,132 KB |
testcase_21 | AC | 252 ms
51,152 KB |
testcase_22 | AC | 388 ms
60,280 KB |
testcase_23 | AC | 225 ms
49,536 KB |
testcase_24 | AC | 170 ms
44,756 KB |
testcase_25 | AC | 281 ms
52,396 KB |
testcase_26 | AC | 243 ms
50,996 KB |
testcase_27 | AC | 398 ms
56,796 KB |
testcase_28 | AC | 201 ms
47,972 KB |
testcase_29 | AC | 343 ms
54,896 KB |
testcase_30 | AC | 263 ms
51,816 KB |
testcase_31 | AC | 331 ms
55,388 KB |
testcase_32 | AC | 221 ms
48,712 KB |
testcase_33 | AC | 579 ms
66,648 KB |
testcase_34 | AC | 499 ms
66,568 KB |
testcase_35 | AC | 485 ms
66,512 KB |
testcase_36 | AC | 53 ms
37,240 KB |
testcase_37 | AC | 53 ms
37,384 KB |
testcase_38 | AC | 53 ms
37,264 KB |
ソースコード
import java.io.*; import java.util.*; import java.util.stream.*; public class Main { static int[] blackCount; static int[] whiteCount; static ArrayList<ArrayList<Integer>> graph = new ArrayList<>(); 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() - 1; int b = sc.nextInt() - 1; graph.get(a).add(b); graph.get(b).add(a); } blackCount = new int[n]; whiteCount = new int[n]; Arrays.fill(blackCount, -1); Arrays.fill(whiteCount, -1); System.out.println(n - Math.max(getBlackCount(0, 0), getWhiteCount(0, 0))); } static int getBlackCount(int idx, int p) { if (blackCount[idx] < 0) { blackCount[idx] = 0; for (int x : graph.get(idx)) { if (x == p) { continue; } blackCount[idx] += Math.max(getBlackCount(x, idx), getWhiteCount(x, idx)); } } return blackCount[idx]; } static int getWhiteCount(int idx, int p) { if (whiteCount[idx] < 0) { whiteCount[idx] = 1; for (int x : graph.get(idx)) { if (x == p) { continue; } whiteCount[idx] += getBlackCount(x, idx); } } return whiteCount[idx]; } } 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(); } }