結果
問題 |
No.2532 Want Play More
|
ユーザー |
![]() |
提出日時 | 2024-04-03 16:20:00 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 941 ms / 2,000 ms |
コード長 | 2,166 bytes |
コンパイル時間 | 2,370 ms |
コンパイル使用メモリ | 80,200 KB |
実行使用メモリ | 119,712 KB |
最終ジャッジ日時 | 2024-09-30 23:59:43 |
合計ジャッジ時間 | 17,634 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 26 |
ソースコード
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(); } } }