結果

問題 No.763 Noelちゃんと木遊び
ユーザー buno15buno15
提出日時 2021-12-04 20:00:44
言語 Java21
(openjdk 21)
結果
AC  
実行時間 524 ms / 2,000 ms
コード長 4,750 bytes
コンパイル時間 2,536 ms
コンパイル使用メモリ 80,660 KB
実行使用メモリ 93,580 KB
最終ジャッジ日時 2023-09-21 08:22:39
合計ジャッジ時間 12,413 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 419 ms
93,580 KB
testcase_01 AC 242 ms
61,588 KB
testcase_02 AC 426 ms
66,540 KB
testcase_03 AC 310 ms
61,904 KB
testcase_04 AC 254 ms
63,588 KB
testcase_05 AC 289 ms
64,448 KB
testcase_06 AC 487 ms
70,676 KB
testcase_07 AC 494 ms
73,616 KB
testcase_08 AC 303 ms
65,324 KB
testcase_09 AC 239 ms
59,636 KB
testcase_10 AC 156 ms
57,800 KB
testcase_11 AC 524 ms
75,520 KB
testcase_12 AC 446 ms
68,072 KB
testcase_13 AC 433 ms
66,928 KB
testcase_14 AC 389 ms
67,668 KB
testcase_15 AC 295 ms
62,156 KB
testcase_16 AC 139 ms
55,524 KB
testcase_17 AC 298 ms
62,704 KB
testcase_18 AC 497 ms
75,236 KB
testcase_19 AC 471 ms
70,388 KB
testcase_20 AC 421 ms
68,056 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Main {

	static final int INF = 1000000000;
	static final long INFL = 1L << 60;
	static final long MOD = 1000000007;
	static final double EPS = 1e-10;

	static int dx[] = { 0, 0, 1, 1, 1, -1, -1, -1 };
	static int dy[] = { 1, -1, 1, 0, -1, 1, 0, -1 };

	static int H;
	static int W;
	static int A;
	static int B;

	public static void main(String args[]) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		IO io = new IO();

		int N = io.getInt();

		ArrayList<ArrayList<Integer>> edge = new ArrayList<>();

		for (int i = 0; i < N; i++) {
			edge.add(new ArrayList<>());
		}

		for (int i = 0; i < N - 1; i++) {
			int arr[] = io.getIntArrPrim();

			int A = arr[0] - 1;
			int B = arr[1] - 1;

			edge.get(A).add(B);
			edge.get(B).add(A);
		}

		int dp[][] = new int[101010][2];

		dfs(edge, dp, 0, -1);

		System.out.println(Math.max(dp[0][0], dp[0][1]));
	}

	static void dfs(ArrayList<ArrayList<Integer>> edge, int dp[][], int now, int parent) {
		dp[now][0] = 1;
		dp[now][1] = 0;

		for (int i = 0; i < edge.get(now).size(); i++) {
			int to = edge.get(now).get(i);
			if (to != parent) {
				dfs(edge, dp, to, now);

				dp[now][0] += Math.max(dp[to][0] - 1, dp[to][1]);
				dp[now][1] += Math.max(dp[to][0], dp[to][1]);
			}
		}
	}
}

class Pair<T1 extends Comparable<? super T1>, T2 extends Comparable<? super T2>> implements Comparable<Pair<T1, T2>> {
	T1 first;
	T2 second;

	public Pair(T1 first, T2 second) {
		this.first = first;
		this.second = second;
	}

	@Override
	public int compareTo(Pair<T1, T2> other) {
		if (first.compareTo(other.first) != 0)
			return first.compareTo(other.first);
		else
			return second.compareTo(other.second);
	}
}

class IO {
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

	public IO() {

	}

	public void println(String str) {
		System.out.println(str);
	}

	public void printArr(Object o[]) {
		for (int i = 0; i < o.length; i++) {
			System.out.print(o + " ");
		}
		System.out.println();
	}

	public int getInt() throws IOException {
		return Integer.parseInt(br.readLine());
	}

	public long getLong() throws IOException {
		return Long.parseLong(br.readLine());
	}

	public double getDouble() throws IOException {
		return Double.parseDouble(br.readLine());
	}

	public String getLine() throws IOException {
		return br.readLine();
	}

	public int[] getIntArrPrim() throws IOException {
		String str[] = br.readLine().split(" ");
		int a[] = new int[str.length];

		for (int i = 0; i < str.length; i++) {
			a[i] = Integer.parseInt(str[i]);
		}

		return a;
	}

	public Integer[] getIntArr() throws IOException {
		String str[] = br.readLine().split(" ");
		Integer a[] = new Integer[str.length];

		for (int i = 0; i < str.length; i++) {
			a[i] = Integer.parseInt(str[i]);
		}

		return a;
	}

	public Long[] getLongArr() throws IOException {
		String str[] = br.readLine().split(" ");
		Long a[] = new Long[str.length];

		for (int i = 0; i < str.length; i++) {
			a[i] = Long.parseLong(str[i]);
		}

		return a;
	}

	public long[] getLongArrPrim() throws IOException {
		String str[] = br.readLine().split(" ");
		long a[] = new long[str.length];

		for (int i = 0; i < str.length; i++) {
			a[i] = Long.parseLong(str[i]);
		}

		return a;
	}

	public String[] getStrArr(String split) throws IOException {
		return br.readLine().split(split);
	}

	public char[] getCharArr() throws IOException {
		return br.readLine().toCharArray();
	}

	public int[][] getIntMap(int w, int h, String split) throws IOException {
		int a[][] = new int[h][w];

		for (int i = 0; i < h; i++) {
			String str[] = br.readLine().split(split);
			for (int j = 0; j < w; j++) {
				a[i][j] = Integer.parseInt(str[j]);
			}
		}

		return a;
	}

	public long[][] getLongMap(int w, int h, String split) throws IOException {
		long a[][] = new long[h][w];

		for (int i = 0; i < h; i++) {
			String str[] = br.readLine().split(split);
			for (int j = 0; j < w; j++) {
				a[i][j] = Long.parseLong(str[j]);
			}
		}

		return a;
	}

	public double[][] getDoubleMap(int w, int h, String split) throws IOException {
		double a[][] = new double[h][w];

		for (int i = 0; i < h; i++) {
			String str[] = br.readLine().split(split);
			for (int j = 0; j < w; j++) {
				a[i][j] = Double.parseDouble(str[j]);
			}
		}

		return a;
	}

	public char[][] getCharMap(int w, int h, String split) throws IOException {
		char a[][] = new char[h][w];

		for (int i = 0; i < h; i++) {
			String str[] = br.readLine().split(split);
			for (int j = 0; j < w; j++) {
				a[i][j] = str[j].charAt(0);
			}
		}

		return a;
	}
}
0