結果

問題 No.1098 LCAs
ユーザー tentententen
提出日時 2021-03-12 18:27:59
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,458 bytes
コンパイル時間 2,587 ms
コンパイル使用メモリ 78,924 KB
実行使用メモリ 104,680 KB
最終ジャッジ日時 2024-04-22 08:34:33
合計ジャッジ時間 15,029 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
42,580 KB
testcase_01 AC 56 ms
36,812 KB
testcase_02 AC 55 ms
36,904 KB
testcase_03 AC 56 ms
37,044 KB
testcase_04 AC 57 ms
37,076 KB
testcase_05 AC 56 ms
36,892 KB
testcase_06 AC 57 ms
36,864 KB
testcase_07 AC 56 ms
36,552 KB
testcase_08 AC 57 ms
36,756 KB
testcase_09 AC 56 ms
36,844 KB
testcase_10 AC 56 ms
37,016 KB
testcase_11 AC 58 ms
37,076 KB
testcase_12 AC 56 ms
36,952 KB
testcase_13 AC 94 ms
38,704 KB
testcase_14 AC 96 ms
38,948 KB
testcase_15 AC 97 ms
38,440 KB
testcase_16 AC 91 ms
38,296 KB
testcase_17 AC 92 ms
38,368 KB
testcase_18 AC 994 ms
80,340 KB
testcase_19 AC 1,274 ms
84,488 KB
testcase_20 AC 1,172 ms
87,240 KB
testcase_21 AC 1,176 ms
81,308 KB
testcase_22 AC 1,254 ms
87,116 KB
testcase_23 TLE -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
権限があれば一括ダウンロードができます

ソースコード

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) {
       long count = 1;
       ArrayList<Integer> unders = new ArrayList<>();
       for (int x : graph.get(idx)) {
           if (x == parent) {
               continue;
           }
           int y = getChildren(x, idx);
           unders.add(y);
           count += y;
           ans[idx] += y * 2;
       }
       for (int i = 0; i < unders.size() - 1; i++) {
           for (int j = i + 1; j < unders.size(); j++) {
               int x = unders.get(i);
               ans[idx] += (long)x * unders.get(j) * 2;
           }
       }
       ans[idx]++;
       return (int)count;
   }
}

0