結果

問題 No.2532 Want Play More
ユーザー ks2mks2m
提出日時 2023-11-03 22:57:42
言語 Java21
(openjdk 21)
結果
AC  
実行時間 845 ms / 2,000 ms
コード長 1,626 bytes
コンパイル時間 2,685 ms
コンパイル使用メモリ 78,172 KB
実行使用メモリ 114,408 KB
最終ジャッジ日時 2023-11-03 22:58:02
合計ジャッジ時間 15,281 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
53,376 KB
testcase_01 AC 838 ms
91,576 KB
testcase_02 AC 805 ms
93,452 KB
testcase_03 AC 828 ms
90,668 KB
testcase_04 AC 812 ms
93,336 KB
testcase_05 AC 746 ms
92,788 KB
testcase_06 AC 590 ms
114,408 KB
testcase_07 AC 112 ms
55,528 KB
testcase_08 AC 133 ms
58,228 KB
testcase_09 AC 125 ms
58,144 KB
testcase_10 AC 412 ms
73,216 KB
testcase_11 AC 437 ms
75,516 KB
testcase_12 AC 604 ms
80,072 KB
testcase_13 AC 502 ms
75,656 KB
testcase_14 AC 223 ms
63,684 KB
testcase_15 AC 845 ms
91,624 KB
testcase_16 AC 790 ms
84,884 KB
testcase_17 AC 133 ms
55,848 KB
testcase_18 AC 582 ms
78,048 KB
testcase_19 AC 765 ms
88,892 KB
testcase_20 AC 48 ms
53,384 KB
testcase_21 AC 56 ms
52,328 KB
testcase_22 AC 49 ms
52,264 KB
testcase_23 AC 48 ms
51,352 KB
testcase_24 AC 47 ms
53,376 KB
testcase_25 AC 48 ms
53,376 KB
testcase_26 AC 48 ms
52,304 KB
testcase_27 AC 48 ms
53,380 KB
testcase_28 AC 646 ms
96,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
	static int n;
	static List<List<Integer>> list;
	static int[] min, max, mini, maxi;

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] sa = br.readLine().split(" ");
		n = Integer.parseInt(sa[0]);
		list = new ArrayList<>(n);
		for (int i = 0; i < n; i++) {
			list.add(new ArrayList<>());
		}
		for (int i = 0; i < n - 1; i++) {
			sa = br.readLine().split(" ");
			int a = Integer.parseInt(sa[0]) - 1;
			int b = Integer.parseInt(sa[1]) - 1;
			list.get(a).add(b);
			list.get(b).add(a);
		}
		br.close();

		min = new int[n];
		Arrays.fill(min, 100000000);
		max = new int[n];
		mini = new int[n];
		maxi = new int[n];
		Arrays.fill(mini, -1);
		Arrays.fill(maxi, -1);
		dfs(0, -1);

		int x = 0;
		int ans = -1;
		while (x != -1) {
			x = maxi[x];
			ans++;
			if (x == -1) {
				break;
			}
			x = mini[x];
			ans++;
		}
		System.out.println(ans);

		x = 0;
		ans = -1;
		while (x != -1) {
			x = mini[x];
			ans++;
			if (x == -1) {
				break;
			}
			x = maxi[x];
			ans++;
		}
		System.out.println(ans);
	}

	static void dfs(int x, int p) {
		for (int i : list.get(x)) {
			if (i == p) {
				continue;
			}
			dfs(i, x);
			if (max[i] <= min[x]) {
				min[x] = max[i];
				mini[x] = i;
			}
			if (min[i] >= max[x]) {
				max[x] = min[i];
				maxi[x] = i;
			}
		}
		if (list.get(x).size() == 1) {
			min[x] = 0;
		} else {
			min[x]++;
			max[x]++;
		}
	}
}
0