結果

問題 No.1098 LCAs
ユーザー tentententen
提出日時 2023-02-01 13:53:21
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,417 bytes
コンパイル時間 2,685 ms
コンパイル使用メモリ 82,208 KB
実行使用メモリ 175,068 KB
最終ジャッジ日時 2023-09-14 01:54:03
合計ジャッジ時間 26,565 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
57,920 KB
testcase_01 AC 58 ms
50,820 KB
testcase_02 AC 57 ms
50,768 KB
testcase_03 AC 57 ms
50,760 KB
testcase_04 AC 58 ms
50,808 KB
testcase_05 AC 58 ms
50,828 KB
testcase_06 AC 58 ms
50,864 KB
testcase_07 AC 58 ms
50,768 KB
testcase_08 AC 59 ms
50,980 KB
testcase_09 AC 58 ms
50,800 KB
testcase_10 AC 58 ms
50,764 KB
testcase_11 AC 59 ms
50,848 KB
testcase_12 AC 58 ms
50,868 KB
testcase_13 AC 99 ms
52,768 KB
testcase_14 AC 96 ms
52,420 KB
testcase_15 AC 99 ms
53,144 KB
testcase_16 AC 97 ms
52,844 KB
testcase_17 AC 97 ms
52,260 KB
testcase_18 AC 1,484 ms
144,296 KB
testcase_19 AC 1,476 ms
143,676 KB
testcase_20 AC 1,502 ms
143,860 KB
testcase_21 AC 1,447 ms
144,148 KB
testcase_22 AC 1,477 ms
144,172 KB
testcase_23 AC 1,231 ms
144,532 KB
testcase_24 AC 1,170 ms
145,044 KB
testcase_25 AC 1,195 ms
144,180 KB
testcase_26 AC 1,089 ms
142,212 KB
testcase_27 AC 1,100 ms
141,960 KB
testcase_28 AC 1,782 ms
164,536 KB
testcase_29 TLE -
testcase_30 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static long[] counts;
    static long[] ans;
    static ArrayList<HashSet<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 HashSet<>());
        }
        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;
        graph.get(idx).remove(p);
        for (int x : graph.get(idx)) {
            counts[idx] += getCount(x, idx);
        }
        ans[idx] = counts[idx];
        for (int x : graph.get(idx)) {
            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