結果

問題 No.763 Noelちゃんと木遊び
ユーザー htensaihtensai
提出日時 2020-06-10 16:57:08
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,062 ms / 2,000 ms
コード長 1,167 bytes
コンパイル時間 2,737 ms
コンパイル使用メモリ 75,752 KB
実行使用メモリ 101,632 KB
最終ジャッジ日時 2023-09-05 12:34:02
合計ジャッジ時間 23,300 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 919 ms
101,632 KB
testcase_01 AC 593 ms
65,160 KB
testcase_02 AC 942 ms
78,940 KB
testcase_03 AC 716 ms
70,052 KB
testcase_04 AC 588 ms
64,900 KB
testcase_05 AC 719 ms
69,684 KB
testcase_06 AC 985 ms
80,344 KB
testcase_07 AC 1,003 ms
80,888 KB
testcase_08 AC 737 ms
70,940 KB
testcase_09 AC 579 ms
64,928 KB
testcase_10 AC 385 ms
62,620 KB
testcase_11 AC 1,062 ms
78,528 KB
testcase_12 AC 1,003 ms
78,588 KB
testcase_13 AC 955 ms
78,468 KB
testcase_14 AC 886 ms
72,480 KB
testcase_15 AC 715 ms
70,548 KB
testcase_16 AC 336 ms
60,492 KB
testcase_17 AC 728 ms
70,956 KB
testcase_18 AC 1,034 ms
80,848 KB
testcase_19 AC 1,001 ms
78,984 KB
testcase_20 AC 1,016 ms
78,848 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[n][2];
		System.out.println(Math.max(dfw(0, 0, 0) , dfw(0, 0, 1)));
	}
	
	static int dfw(int idx, int parent, int flag) {
	    if (dp[idx][flag] != 0) {
	        return dp[idx][flag];
	    }
	    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[idx][flag] = sum;
	}
}
0