結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー tentententen
提出日時 2023-01-12 14:28:50
言語 Java21
(openjdk 21)
結果
AC  
実行時間 573 ms / 2,000 ms
コード長 2,266 bytes
コンパイル時間 2,473 ms
コンパイル使用メモリ 87,800 KB
実行使用メモリ 91,216 KB
最終ジャッジ日時 2023-08-24 21:37:32
合計ジャッジ時間 14,001 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,408 KB
testcase_01 AC 42 ms
49,456 KB
testcase_02 AC 43 ms
49,688 KB
testcase_03 AC 564 ms
77,416 KB
testcase_04 AC 557 ms
76,888 KB
testcase_05 AC 540 ms
76,912 KB
testcase_06 AC 573 ms
77,392 KB
testcase_07 AC 568 ms
77,360 KB
testcase_08 AC 323 ms
65,076 KB
testcase_09 AC 220 ms
58,348 KB
testcase_10 AC 226 ms
60,272 KB
testcase_11 AC 173 ms
56,504 KB
testcase_12 AC 289 ms
62,844 KB
testcase_13 AC 302 ms
64,736 KB
testcase_14 AC 316 ms
64,852 KB
testcase_15 AC 282 ms
62,840 KB
testcase_16 AC 127 ms
55,064 KB
testcase_17 AC 129 ms
54,692 KB
testcase_18 AC 434 ms
67,844 KB
testcase_19 AC 167 ms
55,660 KB
testcase_20 AC 113 ms
52,364 KB
testcase_21 AC 248 ms
62,660 KB
testcase_22 AC 228 ms
60,780 KB
testcase_23 AC 122 ms
52,364 KB
testcase_24 AC 120 ms
52,212 KB
testcase_25 AC 102 ms
52,324 KB
testcase_26 AC 107 ms
52,380 KB
testcase_27 AC 59 ms
50,608 KB
testcase_28 AC 72 ms
51,612 KB
testcase_29 AC 124 ms
54,692 KB
testcase_30 AC 123 ms
52,940 KB
testcase_31 AC 100 ms
52,056 KB
testcase_32 AC 113 ms
52,464 KB
testcase_33 AC 173 ms
63,096 KB
testcase_34 AC 408 ms
91,216 KB
testcase_35 AC 230 ms
70,132 KB
testcase_36 AC 149 ms
55,396 KB
testcase_37 AC 359 ms
67,956 KB
testcase_38 AC 339 ms
67,076 KB
testcase_39 AC 43 ms
49,464 KB
testcase_40 AC 43 ms
49,600 KB
testcase_41 AC 43 ms
47,560 KB
testcase_42 AC 43 ms
49,312 KB
testcase_43 AC 44 ms
49,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static int n;
    static ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
    static long total = 0;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        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);
        }
        getCount(0, 0);
        System.out.println(total);
    }
    
    static long getCount(int idx, int p) {
        long count = 1;
        for (int x : graph.get(idx)) {
            if (x != p) {
                long tmp = getCount(x, idx);
                total += (n - tmp) * tmp;
                count += tmp;
            }
        }
        total += (n - count) * count;
        total += n;
        return count;
    }
    
}

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