結果

問題 No.2532 Want Play More
ユーザー tentententen
提出日時 2024-04-03 16:20:00
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,134 ms / 2,000 ms
コード長 2,166 bytes
コンパイル時間 3,900 ms
コンパイル使用メモリ 79,588 KB
実行使用メモリ 118,440 KB
最終ジャッジ日時 2024-04-03 16:20:22
合計ジャッジ時間 20,739 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
51,920 KB
testcase_01 AC 1,127 ms
92,808 KB
testcase_02 AC 1,019 ms
92,656 KB
testcase_03 AC 919 ms
92,360 KB
testcase_04 AC 933 ms
92,580 KB
testcase_05 AC 907 ms
92,236 KB
testcase_06 AC 1,134 ms
118,440 KB
testcase_07 AC 128 ms
55,928 KB
testcase_08 AC 151 ms
58,432 KB
testcase_09 AC 155 ms
58,364 KB
testcase_10 AC 554 ms
74,508 KB
testcase_11 AC 593 ms
71,936 KB
testcase_12 AC 769 ms
80,736 KB
testcase_13 AC 655 ms
77,172 KB
testcase_14 AC 300 ms
64,828 KB
testcase_15 AC 995 ms
92,520 KB
testcase_16 AC 900 ms
89,200 KB
testcase_17 AC 148 ms
58,128 KB
testcase_18 AC 771 ms
79,572 KB
testcase_19 AC 884 ms
91,864 KB
testcase_20 AC 56 ms
53,988 KB
testcase_21 AC 56 ms
53,996 KB
testcase_22 AC 55 ms
53,996 KB
testcase_23 AC 55 ms
53,996 KB
testcase_24 AC 55 ms
54,004 KB
testcase_25 AC 55 ms
54,000 KB
testcase_26 AC 55 ms
53,980 KB
testcase_27 AC 55 ms
53,984 KB
testcase_28 AC 719 ms
94,056 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class Main {
    static List<List<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);
        }
        System.out.println(search(0, 0, true));
        System.out.println(search(0, 0, false));
    }
    
    static int search(int idx, int p, boolean bigger) {
        if (bigger) {
            int ans = 0;
            for (int x : graph.get(idx)) {
                if (x != p) {
                    ans = Math.max(ans, search(x, idx, bigger ^ true) + 1);
                }
            }
            return ans;
        } else {
            int ans = Integer.MAX_VALUE;
            for (int x : graph.get(idx)) {
                if (x != p) {
                    ans = Math.min(ans, search(x, idx, bigger ^ true) + 1);
                }
            }
            return ans == Integer.MAX_VALUE ? 0 : ans;
        }
    }
}
class Scanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            
        }
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }
    
    public long nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public String next() {
        try {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return st.nextToken();
        }
    }
    
}
0