結果

問題 No.1424 Ultrapalindrome
ユーザー 小野寺健小野寺健
提出日時 2021-05-20 15:10:09
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,514 bytes
コンパイル時間 3,272 ms
コンパイル使用メモリ 78,532 KB
実行使用メモリ 87,200 KB
最終ジャッジ日時 2024-04-18 14:03:37
合計ジャッジ時間 15,174 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
60,996 KB
testcase_01 AC 125 ms
54,104 KB
testcase_02 AC 123 ms
54,076 KB
testcase_03 AC 121 ms
53,868 KB
testcase_04 AC 127 ms
53,980 KB
testcase_05 AC 123 ms
53,924 KB
testcase_06 AC 126 ms
53,944 KB
testcase_07 WA -
testcase_08 AC 126 ms
54,188 KB
testcase_09 AC 969 ms
80,076 KB
testcase_10 AC 977 ms
80,060 KB
testcase_11 AC 796 ms
74,548 KB
testcase_12 AC 925 ms
79,632 KB
testcase_13 AC 518 ms
61,464 KB
testcase_14 AC 854 ms
73,368 KB
testcase_15 AC 162 ms
54,252 KB
testcase_16 AC 722 ms
67,876 KB
testcase_17 AC 809 ms
74,312 KB
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;

public class No1424 {
	private static int N;
	private static int[] P;
	private static List<Integer[]> Edge;
	private static List<Integer> S;

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		N = scan.nextInt();
		P = new int[N];
		Edge = new ArrayList<Integer[]>();
		for (int i=0; i < N-1; i++) {
			int v = scan.nextInt() - 1;
			int u = scan.nextInt() - 1;
			P[v]++;
			P[u]++;
			Edge.add(new Integer[] {v, u});
			Edge.add(new Integer[] {u, v});
		}
		scan.close();
		S = new ArrayList<Integer>();
		for (int i=0; i < N; i++) {
			if (P[i] == 1) {
				S.add(i);
			}
		}
		int res = -1;
		for (int i=0; i < S.size()-1; i++) {
			int v = getPath(S.get(i), i);
			if (v < 0) {
				System.out.println("No");
				return;
			} else if (res < 0) {
				res = v;
			} else if (res != v) {
				System.out.println("No");
			}
		}
		System.out.println("Yes");
	}
	private static int getPath(int s, int i) {
		int[] D = new int[N];
		Arrays.fill(D, Integer.MAX_VALUE);
		D[s] = 0;
		while (true) {
			boolean update = false;
			for (Integer[] e : Edge) {
				if (D[e[0]] != Integer.MAX_VALUE && D[e[1]] > D[e[0]] + 1) {
					D[e[1]] = D[e[0]] + 1;
					update = true;
				}
			}
			if (!update) {
				break;
			}
		}
		int res = -1;
		for (int e : S.subList(i+1, S.size())) {
			if (res < 0) {
				res = D[e];
			} else if (res != D[e]) {
				return -1;
			}
		}
		return res;
	}
}
0