結果

問題 No.1098 LCAs
ユーザー tentententen
提出日時 2023-02-01 13:50:05
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,519 bytes
コンパイル時間 4,599 ms
コンパイル使用メモリ 94,760 KB
実行使用メモリ 116,116 KB
最終ジャッジ日時 2023-09-14 01:51:14
合計ジャッジ時間 24,527 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
35,388 KB
testcase_01 AC 57 ms
35,648 KB
testcase_02 AC 55 ms
35,416 KB
testcase_03 AC 56 ms
35,596 KB
testcase_04 AC 57 ms
35,252 KB
testcase_05 AC 51 ms
35,244 KB
testcase_06 AC 51 ms
35,412 KB
testcase_07 AC 51 ms
35,020 KB
testcase_08 AC 51 ms
35,280 KB
testcase_09 AC 54 ms
35,056 KB
testcase_10 AC 58 ms
35,156 KB
testcase_11 AC 54 ms
35,068 KB
testcase_12 AC 51 ms
35,092 KB
testcase_13 AC 87 ms
36,744 KB
testcase_14 AC 86 ms
36,632 KB
testcase_15 AC 90 ms
36,496 KB
testcase_16 AC 91 ms
36,664 KB
testcase_17 AC 90 ms
36,348 KB
testcase_18 AC 981 ms
95,704 KB
testcase_19 AC 987 ms
95,120 KB
testcase_20 AC 1,019 ms
98,028 KB
testcase_21 AC 980 ms
96,552 KB
testcase_22 AC 1,052 ms
97,180 KB
testcase_23 AC 1,007 ms
99,836 KB
testcase_24 AC 979 ms
100,636 KB
testcase_25 AC 795 ms
93,724 KB
testcase_26 AC 1,004 ms
102,188 KB
testcase_27 AC 935 ms
100,500 KB
testcase_28 TLE -
testcase_29 TLE -
testcase_30 AC 1,343 ms
114,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static long[] counts;
    static long[] 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);
        }
        counts = new long[n];
        ans = new long[n];
        getCount(0, 0);
        System.out.println(String.join("\n", Arrays.stream(ans).mapToObj(String::valueOf).toArray(String[]::new)));
    }
    
    static long getCount(int idx, int p) {
        counts[idx] = 1;
        for (int x : graph.get(idx)) {
            if (x == p) {
                continue;
            }
            counts[idx] += getCount(x, idx);
        }
        ans[idx] = counts[idx];
        for (int x : graph.get(idx)) {
            if (x == p) {
                continue;
            }
            ans[idx] += counts[x] * (counts[idx] - counts[x]);
        }
        return counts[idx];
    }
}
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