結果

問題 No.1582 Vertexes vs Edges
ユーザー tentententen
提出日時 2021-07-08 13:22:51
言語 Java21
(openjdk 21)
結果
AC  
実行時間 537 ms / 2,000 ms
コード長 2,122 bytes
コンパイル時間 2,299 ms
コンパイル使用メモリ 76,052 KB
実行使用メモリ 78,268 KB
最終ジャッジ日時 2023-09-14 04:47:28
合計ジャッジ時間 12,613 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
49,376 KB
testcase_01 AC 41 ms
49,372 KB
testcase_02 AC 41 ms
49,320 KB
testcase_03 AC 44 ms
49,300 KB
testcase_04 AC 121 ms
53,088 KB
testcase_05 AC 327 ms
74,028 KB
testcase_06 AC 121 ms
52,564 KB
testcase_07 AC 138 ms
55,404 KB
testcase_08 AC 218 ms
60,176 KB
testcase_09 AC 303 ms
69,800 KB
testcase_10 AC 236 ms
64,220 KB
testcase_11 AC 127 ms
54,832 KB
testcase_12 AC 202 ms
57,684 KB
testcase_13 AC 253 ms
61,484 KB
testcase_14 AC 267 ms
62,960 KB
testcase_15 AC 347 ms
66,716 KB
testcase_16 AC 230 ms
59,996 KB
testcase_17 AC 250 ms
63,436 KB
testcase_18 AC 354 ms
73,812 KB
testcase_19 AC 256 ms
62,832 KB
testcase_20 AC 263 ms
61,260 KB
testcase_21 AC 242 ms
60,724 KB
testcase_22 AC 341 ms
71,084 KB
testcase_23 AC 210 ms
58,136 KB
testcase_24 AC 163 ms
55,196 KB
testcase_25 AC 253 ms
61,212 KB
testcase_26 AC 219 ms
58,520 KB
testcase_27 AC 337 ms
66,740 KB
testcase_28 AC 194 ms
57,544 KB
testcase_29 AC 290 ms
63,508 KB
testcase_30 AC 243 ms
61,080 KB
testcase_31 AC 305 ms
64,652 KB
testcase_32 AC 192 ms
58,020 KB
testcase_33 AC 532 ms
78,268 KB
testcase_34 AC 406 ms
73,964 KB
testcase_35 AC 537 ms
76,312 KB
testcase_36 AC 41 ms
49,424 KB
testcase_37 AC 40 ms
49,380 KB
testcase_38 AC 42 ms
49,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
    static int[] dpWhite;
    static int[] dpBlack;
    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);
        }
        dpWhite = new int[n];
        dpBlack = new int[n];
        Arrays.fill(dpWhite, -1);
        Arrays.fill(dpBlack, -1);
        System.out.println(n - Math.max(dfwBlack(0, 0), dfwWhite(0, 0)));
    }
    
    static int dfwBlack(int idx, int parent) {
        if (dpBlack[idx] >= 0) {
            return dpBlack[idx];
        }
        dpBlack[idx] = 0;
        for (int x : graph.get(idx)) {
            if (x == parent) {
                continue;
            }
            dpBlack[idx] += Math.max(dfwBlack(x, idx), dfwWhite(x, idx));
        }
        return dpBlack[idx];
    }
    
    static int dfwWhite(int idx, int parent) {
        if (dpWhite[idx] >= 0) {
            return dpWhite[idx];
        }
        dpWhite[idx] = 1;
        for (int x : graph.get(idx)) {
            if (x == parent) {
                continue;
            }
            dpWhite[idx] += dfwBlack(x, idx);
        }
        return dpWhite[idx];
    }
}


class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0