結果

問題 No.763 Noelちゃんと木遊び
ユーザー htensaihtensai
提出日時 2020-06-10 17:00:21
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,047 ms / 2,000 ms
コード長 1,221 bytes
コンパイル時間 2,912 ms
コンパイル使用メモリ 75,740 KB
実行使用メモリ 100,732 KB
最終ジャッジ日時 2023-09-05 12:38:01
合計ジャッジ時間 21,227 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 851 ms
100,732 KB
testcase_01 AC 590 ms
64,564 KB
testcase_02 AC 904 ms
75,572 KB
testcase_03 AC 707 ms
70,132 KB
testcase_04 AC 581 ms
64,672 KB
testcase_05 AC 680 ms
69,364 KB
testcase_06 AC 965 ms
78,704 KB
testcase_07 AC 935 ms
78,596 KB
testcase_08 AC 690 ms
70,928 KB
testcase_09 AC 606 ms
64,820 KB
testcase_10 AC 371 ms
62,348 KB
testcase_11 AC 1,016 ms
78,784 KB
testcase_12 AC 949 ms
78,456 KB
testcase_13 AC 918 ms
76,904 KB
testcase_14 AC 856 ms
72,976 KB
testcase_15 AC 693 ms
69,500 KB
testcase_16 AC 324 ms
60,636 KB
testcase_17 AC 697 ms
69,288 KB
testcase_18 AC 1,047 ms
78,940 KB
testcase_19 AC 987 ms
78,568 KB
testcase_20 AC 969 ms
76,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
    static int[][] dp;
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		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);
		}
		dp = new int[2][n];
		Arrays.fill(dp[0], - 1);
		Arrays.fill(dp[1], - 1);
		System.out.println(Math.max(dfw(0, 0, 0) , dfw(0, 0, 1)));
	}
	
	static int dfw(int idx, int parent, int flag) {
	    if (dp[flag][idx] >= 0) {
	        return dp[flag][idx];
	    }
	    int sum = 0;
	    if (flag == 0) {
	        for (int x : graph.get(idx)) {
	            if (x == parent) {
	                continue;
	            }
	            sum += Math.max(dfw(x, idx, 0), dfw(x, idx, 1));
	        }
	    } else {
	        sum++;
	        for (int x : graph.get(idx)) {
	            if (x == parent) {
	                continue;
	            }
	            sum += Math.max(dfw(x, idx, 0), dfw(x, idx, 1) - 1);
	        }
	    }
	    return dp[flag][idx] = sum;
	}
}
0