結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー tentententen
提出日時 2021-03-06 14:39:51
言語 Java21
(openjdk 21)
結果
AC  
実行時間 925 ms / 2,000 ms
コード長 1,027 bytes
コンパイル時間 2,194 ms
コンパイル使用メモリ 78,724 KB
実行使用メモリ 100,388 KB
最終ジャッジ日時 2024-04-17 03:38:46
合計ジャッジ時間 22,195 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 116 ms
54,192 KB
testcase_01 AC 96 ms
52,352 KB
testcase_02 AC 110 ms
54,440 KB
testcase_03 AC 892 ms
82,936 KB
testcase_04 AC 847 ms
83,036 KB
testcase_05 AC 925 ms
82,596 KB
testcase_06 AC 869 ms
83,560 KB
testcase_07 AC 887 ms
82,832 KB
testcase_08 AC 700 ms
69,156 KB
testcase_09 AC 485 ms
61,572 KB
testcase_10 AC 492 ms
63,792 KB
testcase_11 AC 349 ms
60,636 KB
testcase_12 AC 568 ms
66,804 KB
testcase_13 AC 708 ms
66,704 KB
testcase_14 AC 600 ms
67,792 KB
testcase_15 AC 627 ms
64,100 KB
testcase_16 AC 246 ms
58,744 KB
testcase_17 AC 246 ms
58,940 KB
testcase_18 AC 898 ms
82,252 KB
testcase_19 AC 428 ms
61,428 KB
testcase_20 AC 194 ms
57,784 KB
testcase_21 AC 598 ms
64,044 KB
testcase_22 AC 552 ms
63,680 KB
testcase_23 AC 210 ms
58,016 KB
testcase_24 AC 212 ms
57,812 KB
testcase_25 AC 199 ms
57,556 KB
testcase_26 AC 204 ms
57,988 KB
testcase_27 AC 166 ms
54,572 KB
testcase_28 AC 187 ms
56,632 KB
testcase_29 AC 248 ms
58,600 KB
testcase_30 AC 216 ms
58,060 KB
testcase_31 AC 204 ms
57,508 KB
testcase_32 AC 210 ms
57,768 KB
testcase_33 AC 431 ms
66,220 KB
testcase_34 AC 850 ms
100,388 KB
testcase_35 AC 509 ms
72,060 KB
testcase_36 AC 319 ms
59,172 KB
testcase_37 AC 765 ms
75,496 KB
testcase_38 AC 720 ms
75,012 KB
testcase_39 AC 109 ms
53,764 KB
testcase_40 AC 110 ms
54,136 KB
testcase_41 AC 109 ms
54,540 KB
testcase_42 AC 114 ms
52,984 KB
testcase_43 AC 99 ms
53,004 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static long ans   = 0;
    static int n;
    static ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        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);
        }
        getChild(0, 0);
        ans += (long)n * n;
        System.out.println(ans);
    }
    
    static int getChild(int idx, int parent) {
        int count = 1;
        for (int x : graph.get(idx)) {
            if (x == parent) {
                continue;
            }
            int child = getChild(x, idx);
            count += child;
            ans += (long)child * (n - child);
        }
        ans += (long)(n - count) * count;
        return count;
    }
}
0