結果

問題 No.1098 LCAs
ユーザー tentententen
提出日時 2021-03-12 18:32:14
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,155 bytes
コンパイル時間 2,395 ms
コンパイル使用メモリ 78,212 KB
実行使用メモリ 111,444 KB
最終ジャッジ日時 2024-10-14 07:58:14
合計ジャッジ時間 20,620 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
50,352 KB
testcase_01 AC 54 ms
50,160 KB
testcase_02 AC 54 ms
50,332 KB
testcase_03 AC 54 ms
50,160 KB
testcase_04 AC 54 ms
50,272 KB
testcase_05 AC 56 ms
50,292 KB
testcase_06 AC 56 ms
49,896 KB
testcase_07 AC 56 ms
50,116 KB
testcase_08 AC 55 ms
50,288 KB
testcase_09 AC 57 ms
50,268 KB
testcase_10 AC 57 ms
49,992 KB
testcase_11 AC 58 ms
50,252 KB
testcase_12 AC 55 ms
50,308 KB
testcase_13 AC 93 ms
50,944 KB
testcase_14 AC 95 ms
51,232 KB
testcase_15 AC 95 ms
51,028 KB
testcase_16 AC 84 ms
51,156 KB
testcase_17 AC 95 ms
53,100 KB
testcase_18 AC 1,188 ms
94,944 KB
testcase_19 AC 917 ms
91,432 KB
testcase_20 AC 932 ms
89,540 KB
testcase_21 AC 1,134 ms
94,868 KB
testcase_22 AC 917 ms
89,304 KB
testcase_23 AC 972 ms
96,444 KB
testcase_24 AC 1,012 ms
94,524 KB
testcase_25 AC 777 ms
90,516 KB
testcase_26 AC 798 ms
92,780 KB
testcase_27 AC 1,092 ms
99,660 KB
testcase_28 AC 1,852 ms
111,444 KB
testcase_29 TLE -
testcase_30 AC 1,310 ms
106,660 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