結果
問題 | No.1418 Sum of Sum of Subtree Size |
ユーザー |
![]() |
提出日時 | 2021-03-06 14:39:51 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 1,113 ms / 2,000 ms |
コード長 | 1,027 bytes |
コンパイル時間 | 2,571 ms |
コンパイル使用メモリ | 77,968 KB |
実行使用メモリ | 100,948 KB |
最終ジャッジ日時 | 2024-10-08 13:36:23 |
合計ジャッジ時間 | 26,235 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 41 |
ソースコード
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;}}