結果

問題 No.1424 Ultrapalindrome
ユーザー ks2mks2m
提出日時 2021-03-12 22:02:54
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,149 bytes
コンパイル時間 5,574 ms
コンパイル使用メモリ 78,532 KB
実行使用メモリ 66,244 KB
最終ジャッジ日時 2024-04-22 13:00:21
合計ジャッジ時間 13,218 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
36,776 KB
testcase_01 AC 55 ms
37,088 KB
testcase_02 AC 54 ms
36,980 KB
testcase_03 AC 55 ms
36,984 KB
testcase_04 AC 54 ms
36,984 KB
testcase_05 AC 56 ms
36,976 KB
testcase_06 WA -
testcase_07 AC 55 ms
36,848 KB
testcase_08 WA -
testcase_09 AC 516 ms
56,140 KB
testcase_10 AC 499 ms
56,204 KB
testcase_11 AC 353 ms
53,016 KB
testcase_12 AC 587 ms
58,712 KB
testcase_13 AC 221 ms
46,812 KB
testcase_14 AC 384 ms
53,352 KB
testcase_15 AC 62 ms
37,176 KB
testcase_16 AC 330 ms
51,452 KB
testcase_17 AC 370 ms
52,500 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 685 ms
64,408 KB
testcase_28 AC 409 ms
59,060 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 415 ms
66,244 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;
		for (int i = 0; i < n; i++) {
			if (list.get(i).size() == 1) {
				c1++;
			}
			if (list.get(i).size() > 2) {
				c3++;
			}
		}
		if (c1 == 2 && c3 == 0) {
			System.out.println("Yes");
			return;
		}

		int[] d = new int[n];
		Arrays.fill(d, -1);
		Queue<Integer> que = new ArrayDeque<>();
		for (int i = 0; i < n; i++) {
			if (list.get(i).size() == 1) {
				d[i] = 0;
				que.add(i);
			}
		}

		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];
			}
		}

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

		d = new int[n];
		Arrays.fill(d, -1);
		d[c] = 0;
		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;
				}
			}
		}

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