結果

問題 No.1424 Ultrapalindrome
ユーザー 小野寺健
提出日時 2021-05-20 15:11:19
言語 Java
(openjdk 23)
結果
TLE  
実行時間 -
コード長 1,527 bytes
コンパイル時間 3,664 ms
コンパイル使用メモリ 78,560 KB
実行使用メモリ 82,660 KB
最終ジャッジ日時 2024-10-10 07:15:48
合計ジャッジ時間 16,077 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 15 TLE * 1 -- * 13
権限があれば一括ダウンロードができます

ソースコード

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");
				return;
			}
		}
		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