結果

問題 No.763 Noelちゃんと木遊び
ユーザー tentententen
提出日時 2023-11-15 20:44:24
言語 Java21
(openjdk 21)
結果
AC  
実行時間 565 ms / 2,000 ms
コード長 2,305 bytes
コンパイル時間 4,476 ms
コンパイル使用メモリ 91,256 KB
実行使用メモリ 105,236 KB
最終ジャッジ日時 2023-11-15 20:44:40
合計ジャッジ時間 13,867 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 533 ms
105,236 KB
testcase_01 AC 266 ms
66,832 KB
testcase_02 AC 451 ms
70,248 KB
testcase_03 AC 346 ms
68,536 KB
testcase_04 AC 280 ms
66,788 KB
testcase_05 AC 324 ms
66,760 KB
testcase_06 AC 565 ms
74,348 KB
testcase_07 AC 496 ms
71,296 KB
testcase_08 AC 357 ms
68,724 KB
testcase_09 AC 275 ms
64,688 KB
testcase_10 AC 194 ms
59,220 KB
testcase_11 AC 549 ms
74,468 KB
testcase_12 AC 478 ms
70,484 KB
testcase_13 AC 469 ms
70,440 KB
testcase_14 AC 442 ms
70,004 KB
testcase_15 AC 326 ms
66,880 KB
testcase_16 AC 161 ms
58,776 KB
testcase_17 AC 343 ms
68,656 KB
testcase_18 AC 523 ms
74,556 KB
testcase_19 AC 477 ms
70,556 KB
testcase_20 AC 510 ms
70,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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