結果

問題 No.1098 LCAs
ユーザー tentententen
提出日時 2021-03-12 18:32:14
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,893 ms / 2,000 ms
コード長 1,155 bytes
コンパイル時間 2,414 ms
コンパイル使用メモリ 77,840 KB
実行使用メモリ 113,244 KB
最終ジャッジ日時 2024-04-22 08:44:57
合計ジャッジ時間 19,460 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
37,048 KB
testcase_01 AC 48 ms
37,280 KB
testcase_02 AC 51 ms
37,340 KB
testcase_03 AC 50 ms
37,168 KB
testcase_04 AC 48 ms
37,076 KB
testcase_05 AC 49 ms
37,260 KB
testcase_06 AC 48 ms
36,940 KB
testcase_07 AC 49 ms
37,076 KB
testcase_08 AC 49 ms
37,260 KB
testcase_09 AC 50 ms
37,096 KB
testcase_10 AC 48 ms
37,272 KB
testcase_11 AC 50 ms
37,164 KB
testcase_12 AC 50 ms
36,692 KB
testcase_13 AC 86 ms
38,104 KB
testcase_14 AC 83 ms
38,312 KB
testcase_15 AC 82 ms
38,184 KB
testcase_16 AC 76 ms
38,796 KB
testcase_17 AC 73 ms
38,564 KB
testcase_18 AC 957 ms
84,200 KB
testcase_19 AC 940 ms
83,960 KB
testcase_20 AC 1,016 ms
78,976 KB
testcase_21 AC 961 ms
78,824 KB
testcase_22 AC 842 ms
79,740 KB
testcase_23 AC 880 ms
79,292 KB
testcase_24 AC 655 ms
75,648 KB
testcase_25 AC 852 ms
79,208 KB
testcase_26 AC 890 ms
87,188 KB
testcase_27 AC 918 ms
89,584 KB
testcase_28 AC 1,670 ms
113,244 KB
testcase_29 AC 1,893 ms
108,448 KB
testcase_30 AC 1,057 ms
95,700 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
    static long[] ans;
	public static void main (String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		for (int i = 0; i < n; i++) {
		    graph.add(new ArrayList<>());
		}
		for (int i = 0; i < n - 1; i++) {
		    String[] line = br.readLine().split(" ", 2);
		    int a = Integer.parseInt(line[0]) - 1;
		    int b = Integer.parseInt(line[1]) - 1;
		    graph.get(a).add(b);
		    graph.get(b).add(a);
		}
		ans = new long[n];
		getChildren(0, 0);
		StringBuilder sb = new StringBuilder();
		for (long x : ans) {
		    sb.append(x).append("\n");
		}
        System.out.print(sb);
   }
   
   static int getChildren(int idx, int parent) {
       int count = 1;
       for (int x : graph.get(idx)) {
           if (x == parent) {
               continue;
           }
           int y = getChildren(x, idx);
           ans[idx] += 2L * y * count;
           count += y;
       }
       ans[idx]++;
       return count;
   }
}

0