結果

問題 No.1928 Make a Binary Tree
ユーザー tentententen
提出日時 2022-05-10 08:01:49
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,816 bytes
コンパイル時間 4,385 ms
コンパイル使用メモリ 75,336 KB
実行使用メモリ 177,980 KB
最終ジャッジ日時 2023-09-24 12:09:12
合計ジャッジ時間 27,724 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
49,720 KB
testcase_01 AC 46 ms
49,844 KB
testcase_02 AC 45 ms
49,676 KB
testcase_03 AC 46 ms
50,048 KB
testcase_04 AC 46 ms
49,672 KB
testcase_05 AC 46 ms
49,672 KB
testcase_06 AC 48 ms
49,644 KB
testcase_07 AC 48 ms
49,632 KB
testcase_08 AC 50 ms
49,772 KB
testcase_09 AC 50 ms
49,708 KB
testcase_10 AC 49 ms
49,968 KB
testcase_11 AC 48 ms
49,616 KB
testcase_12 AC 47 ms
50,064 KB
testcase_13 AC 49 ms
49,656 KB
testcase_14 AC 48 ms
49,748 KB
testcase_15 AC 177 ms
55,948 KB
testcase_16 AC 160 ms
55,832 KB
testcase_17 AC 151 ms
55,736 KB
testcase_18 AC 173 ms
55,900 KB
testcase_19 AC 108 ms
52,388 KB
testcase_20 AC 149 ms
55,796 KB
testcase_21 AC 129 ms
54,776 KB
testcase_22 AC 159 ms
55,636 KB
testcase_23 AC 144 ms
55,536 KB
testcase_24 AC 175 ms
55,936 KB
testcase_25 AC 177 ms
56,336 KB
testcase_26 AC 1,191 ms
99,652 KB
testcase_27 AC 759 ms
82,564 KB
testcase_28 AC 844 ms
82,048 KB
testcase_29 AC 1,266 ms
102,700 KB
testcase_30 AC 758 ms
80,416 KB
testcase_31 AC 980 ms
86,172 KB
testcase_32 AC 1,262 ms
101,676 KB
testcase_33 AC 804 ms
83,804 KB
testcase_34 AC 1,104 ms
90,476 KB
testcase_35 AC 669 ms
94,320 KB
testcase_36 TLE -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        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);
        }
        check(0, 0);
        System.out.println(-check(0, 0).poll());
    }
    
    static PriorityQueue<Integer> check(int idx, int p) {
        PriorityQueue<Integer> ans = new PriorityQueue<>();
        for (int x : graph.get(idx)) {
            if (x == p) {
                continue;
            }
            ans.addAll(check(x, idx));
        }
        int value = 0;
        if (ans.size() > 0) {
            value += ans.poll();
        }
        if (ans.size() > 0) {
            value += ans.poll();
        }
        value--;
        ans.add(value);
        return ans;
    }
}

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 double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0