結果

問題 No.1424 Ultrapalindrome
ユーザー tentententen
提出日時 2021-08-05 10:31:56
言語 Java21
(openjdk 21)
結果
AC  
実行時間 692 ms / 2,000 ms
コード長 2,621 bytes
コンパイル時間 6,055 ms
コンパイル使用メモリ 75,536 KB
実行使用メモリ 84,772 KB
最終ジャッジ日時 2023-10-14 21:31:04
合計ジャッジ時間 18,481 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
49,396 KB
testcase_01 AC 45 ms
49,476 KB
testcase_02 AC 45 ms
49,544 KB
testcase_03 AC 45 ms
49,488 KB
testcase_04 AC 45 ms
49,556 KB
testcase_05 AC 45 ms
49,476 KB
testcase_06 AC 46 ms
49,672 KB
testcase_07 AC 45 ms
49,412 KB
testcase_08 AC 45 ms
49,504 KB
testcase_09 AC 326 ms
62,604 KB
testcase_10 AC 332 ms
65,276 KB
testcase_11 AC 272 ms
60,824 KB
testcase_12 AC 325 ms
63,156 KB
testcase_13 AC 182 ms
55,772 KB
testcase_14 AC 289 ms
64,552 KB
testcase_15 AC 50 ms
49,520 KB
testcase_16 AC 260 ms
60,504 KB
testcase_17 AC 281 ms
63,768 KB
testcase_18 AC 367 ms
64,236 KB
testcase_19 AC 474 ms
65,260 KB
testcase_20 AC 212 ms
55,696 KB
testcase_21 AC 380 ms
64,740 KB
testcase_22 AC 294 ms
60,692 KB
testcase_23 AC 158 ms
55,028 KB
testcase_24 AC 200 ms
56,012 KB
testcase_25 AC 196 ms
55,540 KB
testcase_26 AC 541 ms
67,188 KB
testcase_27 AC 553 ms
78,404 KB
testcase_28 AC 424 ms
72,544 KB
testcase_29 AC 554 ms
77,980 KB
testcase_30 AC 581 ms
80,812 KB
testcase_31 AC 692 ms
84,772 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
        }
        for (int i = 0; i < n - 1; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            graph.get(a).add(b);
            graph.get(b).add(a);
        }
        PriorityQueue<Path> queue = new PriorityQueue<>();
        for (int i = 0; i < n; i++) {
            if (graph.get(i).size() > 2) {
                queue.add(new Path(i, 0));
            }
        }
        if (queue.size() == 0) {
            System.out.println("Yes");
            return;
        } else if (queue.size() > 1) {
            System.out.println("No");
            return;
        }
        int[] costs = new int[n];
        Arrays.fill(costs, Integer.MAX_VALUE);
        while (queue.size() > 0) {
            Path p = queue.poll();
            if (costs[p.idx] <= p.value) {
                continue;
            }
            costs[p.idx] = p.value;
            for (int x : graph.get(p.idx)) {
                queue.add(new Path(x, p.value + 1));
            }
        }
        int base = -1;
        for (int i = 0; i < n; i++) {
            if (graph.get(i).size() == 1) {
                if (base == -1 || base == costs[i]) {
                    base = costs[i];
                } else {
                    System.out.println("No");
                    return;
                }
            }
        }
        System.out.println("Yes");
    }
    
    static class Path implements Comparable<Path> {
        int idx;
        int value;
        
        public Path(int idx, int value) {
            this.idx = idx;
            this.value = value;
        }
        
        public int compareTo(Path another) {
            return value - another.value;
        }
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0