結果

問題 No.1098 LCAs
ユーザー tentententen
提出日時 2021-11-24 11:07:47
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,948 ms / 2,000 ms
コード長 1,921 bytes
コンパイル時間 2,180 ms
コンパイル使用メモリ 74,936 KB
実行使用メモリ 112,712 KB
最終ジャッジ日時 2023-09-08 17:49:37
合計ジャッジ時間 21,027 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,396 KB
testcase_01 AC 41 ms
49,508 KB
testcase_02 AC 42 ms
49,440 KB
testcase_03 AC 43 ms
49,244 KB
testcase_04 AC 44 ms
49,600 KB
testcase_05 AC 41 ms
49,600 KB
testcase_06 AC 42 ms
49,236 KB
testcase_07 AC 43 ms
49,912 KB
testcase_08 AC 43 ms
49,488 KB
testcase_09 AC 42 ms
49,296 KB
testcase_10 AC 40 ms
49,540 KB
testcase_11 AC 41 ms
49,468 KB
testcase_12 AC 42 ms
49,284 KB
testcase_13 AC 75 ms
50,512 KB
testcase_14 AC 74 ms
48,580 KB
testcase_15 AC 75 ms
51,300 KB
testcase_16 AC 82 ms
50,968 KB
testcase_17 AC 71 ms
51,536 KB
testcase_18 AC 795 ms
91,808 KB
testcase_19 AC 784 ms
92,604 KB
testcase_20 AC 820 ms
91,480 KB
testcase_21 AC 825 ms
92,152 KB
testcase_22 AC 837 ms
91,180 KB
testcase_23 AC 678 ms
88,340 KB
testcase_24 AC 736 ms
89,188 KB
testcase_25 AC 669 ms
91,488 KB
testcase_26 AC 725 ms
95,172 KB
testcase_27 AC 748 ms
94,860 KB
testcase_28 AC 1,826 ms
110,308 KB
testcase_29 AC 1,948 ms
112,712 KB
testcase_30 AC 1,080 ms
105,140 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    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);
        }
        ans = new long[n];
        getCount(0, 0);
        StringBuilder sb = new StringBuilder();
        for (long x : ans) {
            sb.append(x).append("\n");
        }
        System.out.print(sb);
    }
    
    static int getCount(int idx , int p) {
        int count = 1;
        long powCount = 0;
        for (int x : graph.get(idx)) {
            if (x == p) {
                continue;
            }
            int tmp = getCount(x, idx);
            count += tmp;
            powCount += getPow2(tmp);
        }
        ans[idx] = getPow2(count) - powCount;
        return count;
    }
    
    static long getPow2(long x) {
        return x * x;
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    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 String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0