結果

問題 No.763 Noelちゃんと木遊び
ユーザー tentententen
提出日時 2023-07-07 14:34:38
言語 Java21
(openjdk 21)
結果
AC  
実行時間 522 ms / 2,000 ms
コード長 2,308 bytes
コンパイル時間 3,188 ms
コンパイル使用メモリ 84,088 KB
実行使用メモリ 96,756 KB
最終ジャッジ日時 2023-09-28 15:04:40
合計ジャッジ時間 13,679 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 372 ms
96,756 KB
testcase_01 AC 249 ms
60,992 KB
testcase_02 AC 431 ms
66,944 KB
testcase_03 AC 322 ms
64,492 KB
testcase_04 AC 259 ms
60,564 KB
testcase_05 AC 302 ms
62,672 KB
testcase_06 AC 481 ms
71,060 KB
testcase_07 AC 476 ms
68,204 KB
testcase_08 AC 321 ms
64,724 KB
testcase_09 AC 248 ms
60,716 KB
testcase_10 AC 156 ms
54,844 KB
testcase_11 AC 503 ms
71,216 KB
testcase_12 AC 454 ms
67,224 KB
testcase_13 AC 449 ms
66,968 KB
testcase_14 AC 411 ms
66,628 KB
testcase_15 AC 317 ms
64,740 KB
testcase_16 AC 147 ms
55,272 KB
testcase_17 AC 316 ms
64,840 KB
testcase_18 AC 522 ms
71,256 KB
testcase_19 AC 441 ms
67,004 KB
testcase_20 AC 441 ms
66,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
    static int[] dp1;
    static int[] dp2;
    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);
        }
        dp1 = new int[n];
        dp2 = new int[n];
        check(0, 0);
        System.out.println(Math.max(dp1[0], dp2[0]));
    }
    
    static void check(int idx, int p) {
        dp1[idx] = 1;
        dp2[idx] = 0;
        for (int x : graph.get(idx)) {
            if (x == p) {
                continue;
            }
            check(x, idx);
            dp1[idx] += Math.max(dp1[x] - 1, dp2[x]);
            dp2[idx] += Math.max(dp1[x], dp2[x]);
        }
    }
}
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