結果

問題 No.1424 Ultrapalindrome
ユーザー ks2mks2m
提出日時 2021-03-12 22:08:44
言語 Java21
(openjdk 21)
結果
AC  
実行時間 475 ms / 2,000 ms
コード長 1,644 bytes
コンパイル時間 2,335 ms
コンパイル使用メモリ 78,336 KB
実行使用メモリ 63,832 KB
最終ジャッジ日時 2024-04-22 17:09:02
合計ジャッジ時間 11,269 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
36,496 KB
testcase_01 AC 52 ms
36,788 KB
testcase_02 AC 53 ms
36,920 KB
testcase_03 AC 51 ms
36,840 KB
testcase_04 AC 52 ms
36,840 KB
testcase_05 AC 52 ms
36,848 KB
testcase_06 AC 52 ms
36,892 KB
testcase_07 AC 52 ms
36,784 KB
testcase_08 AC 52 ms
36,760 KB
testcase_09 AC 391 ms
54,516 KB
testcase_10 AC 396 ms
54,592 KB
testcase_11 AC 306 ms
51,496 KB
testcase_12 AC 402 ms
53,952 KB
testcase_13 AC 202 ms
46,012 KB
testcase_14 AC 327 ms
52,312 KB
testcase_15 AC 55 ms
37,196 KB
testcase_16 AC 272 ms
49,704 KB
testcase_17 AC 312 ms
51,356 KB
testcase_18 AC 294 ms
50,016 KB
testcase_19 AC 371 ms
52,580 KB
testcase_20 AC 195 ms
45,164 KB
testcase_21 AC 326 ms
51,408 KB
testcase_22 AC 247 ms
47,640 KB
testcase_23 AC 153 ms
42,744 KB
testcase_24 AC 188 ms
44,812 KB
testcase_25 AC 185 ms
44,916 KB
testcase_26 AC 415 ms
54,324 KB
testcase_27 AC 475 ms
60,776 KB
testcase_28 AC 376 ms
58,760 KB
testcase_29 AC 432 ms
61,092 KB
testcase_30 AC 419 ms
61,836 KB
testcase_31 AC 454 ms
63,832 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] sa = br.readLine().split(" ");
		int n = Integer.parseInt(sa[0]);
		List<List<Integer>> 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();

		int c1 = 0;
		int c3 = 0;
		int c = 0;
		for (int i = 0; i < n; i++) {
			if (list.get(i).size() == 1) {
				c1++;
			}
			if (list.get(i).size() > 2) {
				c3++;
				c = i;
			}
		}
		if (c1 == 2 && c3 == 0) {
			System.out.println("Yes");
			return;
		}
		if (c3 > 1) {
			System.out.println("No");
			return;
		}

		int[] d = new int[n];
		Arrays.fill(d, -1);
		d[c] = 0;
		Queue<Integer> que = new ArrayDeque<>();
		que.add(c);
		while (!que.isEmpty()) {
			int cur = que.poll();
			for (int next : list.get(cur)) {
				if (d[next] == -1) {
					que.add(next);
					d[next] = d[cur] + 1;
				}
			}
		}
		int max = 0;
		for (int i = 0; i < n; i++) {
			if (d[i] > max) {
				max = d[i];
			}
		}

		for (int i = 0; i < n; i++) {
			if (list.get(i).size() == 1) {
				if (d[i] != max) {
					System.out.println("No");
					return;
				}
			}
		}
		System.out.println("Yes");
	}
}
0