結果

問題 No.1424 Ultrapalindrome
ユーザー 小野寺健小野寺健
提出日時 2021-05-20 16:04:05
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,195 bytes
コンパイル時間 3,833 ms
コンパイル使用メモリ 80,312 KB
実行使用メモリ 57,940 KB
最終ジャッジ日時 2024-04-18 14:05:01
合計ジャッジ時間 19,869 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
41,148 KB
testcase_01 WA -
testcase_02 AC 111 ms
41,616 KB
testcase_03 AC 123 ms
41,172 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 135 ms
41,440 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 497 ms
48,144 KB
testcase_19 AC 642 ms
48,112 KB
testcase_20 AC 405 ms
47,896 KB
testcase_21 AC 610 ms
48,456 KB
testcase_22 AC 519 ms
47,924 KB
testcase_23 AC 284 ms
47,528 KB
testcase_24 AC 373 ms
47,792 KB
testcase_25 AC 369 ms
47,768 KB
testcase_26 AC 734 ms
51,460 KB
testcase_27 WA -
testcase_28 AC 691 ms
57,360 KB
testcase_29 WA -
testcase_30 AC 712 ms
52,588 KB
testcase_31 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Comparator;

public class No1424 {
	private static int N;
	private static HashMap<Integer, List<Integer>> Edge;
	private static List<Integer> S;

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		N = scan.nextInt();
		Edge = new HashMap<Integer, List<Integer>>();
		for (int i=0; i < N-1; i++) {
			int v = scan.nextInt() - 1;
			int u = scan.nextInt() - 1;
			Edge.getOrDefault(v, new ArrayList<Integer>()).add(u);
			Edge.getOrDefault(u, new ArrayList<Integer>()).add(v);
		}
		scan.close();
		S = new ArrayList<Integer>();
		for (int i=0; i < N; i++) {
			if (Edge.containsKey(i) && Edge.get(i).size() == 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 class MyComparator implements Comparator {
		 
	    public int compare(Object obj1, Object obj2) {
	         
	        Integer[] num1 = (Integer[])obj1;
	        Integer[] num2 = (Integer[])obj2;
	         
	        if(num1[0] > num2[0]) {
	            return 1;
	        } else if(num1[0] < num2[0]) {
	            return -1;
	        } else{
	            return 0;
	        }
	    }
	}
	
	private static int getPath(int s, int i) {
		int[] D = new int[N];
		Arrays.fill(D, Integer.MAX_VALUE);
		D[s] = 0;
		Queue<Integer[]> q = new PriorityQueue<Integer[]>(new MyComparator());
		q.add(new Integer[] {0, s});
		while (q.size() > 0) {
			Integer[] c = q.poll();
			if (c[0] > D[c[1]]) {
				continue;
			}
			for (int p : Edge.get(c[1])) {
				if (D[p] > D[c[1]] + 1) {
					D[p] = D[c[1]] + 1;
					q.add(new Integer[] {D[p], p});
				}
			}
		}
		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