結果

問題 No.1582 Vertexes vs Edges
ユーザー tentententen
提出日時 2023-02-03 12:56:10
言語 Java21
(openjdk 21)
結果
AC  
実行時間 541 ms / 2,000 ms
コード長 2,834 bytes
コンパイル時間 2,834 ms
コンパイル使用メモリ 82,388 KB
実行使用メモリ 78,108 KB
最終ジャッジ日時 2023-09-15 10:05:25
合計ジャッジ時間 13,224 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,380 KB
testcase_01 AC 43 ms
49,724 KB
testcase_02 AC 43 ms
49,164 KB
testcase_03 AC 46 ms
49,268 KB
testcase_04 AC 108 ms
52,136 KB
testcase_05 AC 337 ms
73,368 KB
testcase_06 AC 121 ms
54,552 KB
testcase_07 AC 142 ms
55,376 KB
testcase_08 AC 223 ms
60,788 KB
testcase_09 AC 322 ms
71,192 KB
testcase_10 AC 243 ms
62,084 KB
testcase_11 AC 130 ms
54,624 KB
testcase_12 AC 209 ms
56,448 KB
testcase_13 AC 259 ms
63,716 KB
testcase_14 AC 273 ms
64,240 KB
testcase_15 AC 340 ms
66,992 KB
testcase_16 AC 224 ms
59,788 KB
testcase_17 AC 266 ms
64,768 KB
testcase_18 AC 371 ms
74,108 KB
testcase_19 AC 259 ms
64,776 KB
testcase_20 AC 259 ms
61,900 KB
testcase_21 AC 240 ms
63,068 KB
testcase_22 AC 344 ms
71,352 KB
testcase_23 AC 211 ms
60,000 KB
testcase_24 AC 154 ms
53,436 KB
testcase_25 AC 242 ms
62,016 KB
testcase_26 AC 219 ms
59,180 KB
testcase_27 AC 351 ms
66,696 KB
testcase_28 AC 185 ms
57,540 KB
testcase_29 AC 299 ms
64,624 KB
testcase_30 AC 236 ms
62,672 KB
testcase_31 AC 310 ms
64,420 KB
testcase_32 AC 197 ms
59,192 KB
testcase_33 AC 504 ms
75,860 KB
testcase_34 AC 541 ms
78,020 KB
testcase_35 AC 517 ms
78,108 KB
testcase_36 AC 43 ms
49,568 KB
testcase_37 AC 43 ms
49,300 KB
testcase_38 AC 43 ms
49,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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();
    }
    
}
0