結果

問題 No.277 根掘り葉掘り
ユーザー tentententen
提出日時 2023-04-04 14:39:06
言語 Java21
(openjdk 21)
結果
AC  
実行時間 824 ms / 3,000 ms
コード長 2,799 bytes
コンパイル時間 2,657 ms
コンパイル使用メモリ 91,476 KB
実行使用メモリ 107,604 KB
最終ジャッジ日時 2024-04-08 14:16:40
合計ジャッジ時間 12,959 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 66 ms
54,644 KB
testcase_01 AC 66 ms
54,640 KB
testcase_02 AC 66 ms
54,640 KB
testcase_03 AC 70 ms
54,628 KB
testcase_04 AC 71 ms
54,644 KB
testcase_05 AC 70 ms
54,628 KB
testcase_06 AC 69 ms
54,640 KB
testcase_07 AC 71 ms
54,632 KB
testcase_08 AC 67 ms
54,620 KB
testcase_09 AC 824 ms
107,604 KB
testcase_10 AC 656 ms
81,340 KB
testcase_11 AC 750 ms
81,408 KB
testcase_12 AC 714 ms
91,860 KB
testcase_13 AC 657 ms
77,404 KB
testcase_14 AC 614 ms
82,408 KB
testcase_15 AC 644 ms
85,448 KB
testcase_16 AC 633 ms
82,572 KB
testcase_17 AC 749 ms
87,368 KB
testcase_18 AC 631 ms
82,312 KB
testcase_19 AC 633 ms
82,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static int[] depths;
    static int[] leaves;
    static int[] ans;
    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);
        }
        depths = new int[n];
        leaves = new int[n];
        setDepth(0, 0, 0);
        ans = new int[n];
        setAns(0, 0, 0);
        System.out.println(String.join("\n", Arrays.stream(ans).mapToObj(String::valueOf).toArray(String[]::new)));
    }
    
    static int setDepth(int idx, int p, int d) {
        depths[idx] = d;
        if (graph.get(idx).size() > 1 || idx == 0) {
            leaves[idx] = Integer.MAX_VALUE;
            for (int x : graph.get(idx)) {
                if (x != p) {
                    leaves[idx] = Math.min(leaves[idx], setDepth(x, idx, d + 1));
                }
            }
        }
        return leaves[idx] + 1;
    }
    
    static void setAns(int idx, int p, int v) {
        ans[idx] = Math.min(Math.min(leaves[idx], depths[idx]), v);
        for (int x : graph.get(idx)) {
            if (x != p) {
                setAns(x, idx, ans[idx] + 1);
            }
        }
    }
    
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    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 int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0