結果

問題 No.2427 Tree Distance Two
ユーザー tentententen
提出日時 2023-08-24 09:00:24
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,291 ms / 2,000 ms
コード長 2,655 bytes
コンパイル時間 5,762 ms
コンパイル使用メモリ 90,500 KB
実行使用メモリ 160,996 KB
最終ジャッジ日時 2023-08-24 09:00:59
合計ジャッジ時間 34,498 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
50,588 KB
testcase_01 AC 58 ms
50,840 KB
testcase_02 AC 59 ms
50,728 KB
testcase_03 AC 1,188 ms
123,044 KB
testcase_04 AC 58 ms
50,644 KB
testcase_05 AC 1,192 ms
122,904 KB
testcase_06 AC 59 ms
50,736 KB
testcase_07 AC 1,265 ms
132,080 KB
testcase_08 AC 1,079 ms
113,040 KB
testcase_09 AC 1,063 ms
115,376 KB
testcase_10 AC 1,288 ms
133,872 KB
testcase_11 AC 1,282 ms
130,488 KB
testcase_12 AC 1,131 ms
119,500 KB
testcase_13 AC 1,158 ms
119,668 KB
testcase_14 AC 913 ms
160,996 KB
testcase_15 AC 60 ms
50,500 KB
testcase_16 AC 62 ms
50,784 KB
testcase_17 AC 59 ms
50,804 KB
testcase_18 AC 62 ms
50,824 KB
testcase_19 AC 59 ms
50,772 KB
testcase_20 AC 169 ms
56,504 KB
testcase_21 AC 187 ms
56,636 KB
testcase_22 AC 126 ms
54,068 KB
testcase_23 AC 109 ms
52,968 KB
testcase_24 AC 176 ms
54,692 KB
testcase_25 AC 519 ms
69,860 KB
testcase_26 AC 971 ms
99,380 KB
testcase_27 AC 892 ms
85,700 KB
testcase_28 AC 1,291 ms
122,952 KB
testcase_29 AC 1,130 ms
110,612 KB
testcase_30 AC 931 ms
90,020 KB
testcase_31 AC 691 ms
78,992 KB
testcase_32 AC 959 ms
101,036 KB
testcase_33 AC 861 ms
87,796 KB
testcase_34 AC 381 ms
66,280 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
    static int[] ans;
    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);
        }
        ans = new int[n];
        calc(0, 0);
        System.out.println(String.join("\n", Arrays.stream(ans).mapToObj(String::valueOf).toArray(String[]::new)));
    }
    
    static void calc(int idx, int p) {
        if (idx > 0) {
            ans[idx] += getParentCount(p);
            ans[idx] += getChildrenCount(p) - 1;
        }
        for (int x : graph.get(idx)) {
            if (p != x) {
                ans[idx] += getChildrenCount(x);
                calc(x, idx);
            }
        }
    }
    
    static int getParentCount(int x) {
        if (x > 0) {
            return 1;
        } else {
            return 0;
        }
    }
    
    static int getChildrenCount(int x) {
        if (x > 0) {
            return graph.get(x).size() - 1;
        } else {
            return graph.get(x).size();
        }
    }
    
} 
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