結果
問題 | No.1418 Sum of Sum of Subtree Size |
ユーザー | tenten |
提出日時 | 2023-07-12 10:03:14 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 611 ms / 2,000 ms |
コード長 | 2,250 bytes |
コンパイル時間 | 2,812 ms |
コンパイル使用メモリ | 88,492 KB |
実行使用メモリ | 92,308 KB |
最終ジャッジ日時 | 2023-10-12 00:12:13 |
合計ジャッジ時間 | 16,892 ms |
ジャッジサーバーID (参考情報) |
judge13 / judge11 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 45 ms
49,432 KB |
testcase_01 | AC | 43 ms
49,504 KB |
testcase_02 | AC | 43 ms
49,512 KB |
testcase_03 | AC | 592 ms
78,952 KB |
testcase_04 | AC | 608 ms
77,112 KB |
testcase_05 | AC | 607 ms
77,256 KB |
testcase_06 | AC | 611 ms
77,072 KB |
testcase_07 | AC | 608 ms
77,192 KB |
testcase_08 | AC | 364 ms
64,904 KB |
testcase_09 | AC | 236 ms
60,108 KB |
testcase_10 | AC | 254 ms
60,132 KB |
testcase_11 | AC | 186 ms
55,892 KB |
testcase_12 | AC | 307 ms
62,920 KB |
testcase_13 | AC | 348 ms
64,992 KB |
testcase_14 | AC | 351 ms
64,612 KB |
testcase_15 | AC | 288 ms
63,200 KB |
testcase_16 | AC | 132 ms
54,744 KB |
testcase_17 | AC | 133 ms
54,388 KB |
testcase_18 | AC | 459 ms
67,608 KB |
testcase_19 | AC | 177 ms
56,152 KB |
testcase_20 | AC | 119 ms
52,428 KB |
testcase_21 | AC | 293 ms
62,760 KB |
testcase_22 | AC | 262 ms
60,672 KB |
testcase_23 | AC | 124 ms
52,664 KB |
testcase_24 | AC | 122 ms
52,520 KB |
testcase_25 | AC | 114 ms
52,412 KB |
testcase_26 | AC | 119 ms
53,216 KB |
testcase_27 | AC | 62 ms
50,636 KB |
testcase_28 | AC | 76 ms
50,624 KB |
testcase_29 | AC | 125 ms
52,404 KB |
testcase_30 | AC | 122 ms
52,436 KB |
testcase_31 | AC | 94 ms
51,980 KB |
testcase_32 | AC | 118 ms
52,512 KB |
testcase_33 | AC | 195 ms
63,216 KB |
testcase_34 | AC | 442 ms
92,308 KB |
testcase_35 | AC | 247 ms
71,116 KB |
testcase_36 | AC | 148 ms
55,396 KB |
testcase_37 | AC | 378 ms
68,292 KB |
testcase_38 | AC | 373 ms
66,976 KB |
testcase_39 | AC | 45 ms
49,456 KB |
testcase_40 | AC | 45 ms
49,352 KB |
testcase_41 | AC | 45 ms
49,348 KB |
testcase_42 | AC | 43 ms
49,456 KB |
testcase_43 | AC | 44 ms
49,568 KB |
ソースコード
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 ans = 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(ans); } static long getCount(int idx, int p) { long count = 1; for (int x : graph.get(idx)) { if (x == p) { continue; } long tmp = getCount(x, idx); ans += tmp * (n - tmp); count += tmp; } ans += (n - count) * count + 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(); } }