結果

問題 No.1098 LCAs
ユーザー ks2mks2m
提出日時 2020-06-26 22:39:45
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,262 ms / 2,000 ms
コード長 1,703 bytes
コンパイル時間 2,295 ms
コンパイル使用メモリ 75,952 KB
実行使用メモリ 116,736 KB
最終ジャッジ日時 2023-09-18 06:16:27
合計ジャッジ時間 20,805 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,576 KB
testcase_01 AC 43 ms
47,700 KB
testcase_02 AC 44 ms
49,636 KB
testcase_03 AC 43 ms
49,276 KB
testcase_04 AC 44 ms
49,272 KB
testcase_05 AC 43 ms
49,304 KB
testcase_06 AC 44 ms
49,108 KB
testcase_07 AC 46 ms
49,480 KB
testcase_08 AC 44 ms
49,480 KB
testcase_09 AC 44 ms
49,072 KB
testcase_10 AC 47 ms
49,408 KB
testcase_11 AC 44 ms
49,408 KB
testcase_12 AC 42 ms
47,776 KB
testcase_13 AC 81 ms
51,480 KB
testcase_14 AC 91 ms
51,240 KB
testcase_15 AC 91 ms
53,428 KB
testcase_16 AC 82 ms
49,712 KB
testcase_17 AC 84 ms
50,936 KB
testcase_18 AC 1,195 ms
103,616 KB
testcase_19 AC 1,131 ms
101,068 KB
testcase_20 AC 1,233 ms
104,376 KB
testcase_21 AC 1,262 ms
104,632 KB
testcase_22 AC 1,145 ms
99,572 KB
testcase_23 AC 1,195 ms
105,180 KB
testcase_24 AC 1,133 ms
100,248 KB
testcase_25 AC 1,173 ms
111,976 KB
testcase_26 AC 1,193 ms
113,660 KB
testcase_27 AC 1,196 ms
116,736 KB
testcase_28 AC 1,168 ms
99,396 KB
testcase_29 AC 1,132 ms
100,356 KB
testcase_30 AC 1,150 ms
99,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Queue;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] sa = br.readLine().split(" ");
		int n = Integer.parseInt(sa[0]);
		List<List<Integer>> list = new ArrayList<>(n);
		for (int i = 0; i < n; i++) {
			list.add(new ArrayList<>());
		}
		for (int i = 0; i < n - 1; i++) {
			sa = br.readLine().split(" ");
			int v = Integer.parseInt(sa[0]) - 1;
			int w = Integer.parseInt(sa[1]) - 1;
			list.get(v).add(w);
			list.get(w).add(v);
		}
		br.close();

		Obj[] ans = new Obj[n];
		List<Integer> order = new ArrayList<>();
		int[] par = new int[n];
		Arrays.fill(par, -2);
		Queue<Integer> que = new ArrayDeque<>();
		que.add(0);
		order.add(0);
		par[0] = -1;
		while (!que.isEmpty()) {
			int cur = que.poll();
			for (int next : list.get(cur)) {
				if (par[next] == -2) {
					que.add(next);
					order.add(next);
					par[next] = cur;
				}
			}
		}
		Collections.reverse(order);

		for (int i : order) {
			Obj o = new Obj();
			long total = 0;
			for (int c : list.get(i)) {
				if (c != par[i]) {
					o.size += ans[c].size;
					total += ans[c].total;
				}
			}
			o.size++;
			o.total = (long) o.size * o.size;
			o.val = o.total - total;
			ans[i] = o;
		}

		PrintWriter pw = new PrintWriter(System.out);
		for (Obj o : ans) {
			pw.println(o.val);
		}
		pw.flush();
	}

	static class Obj {
		int size;
		long val, total;
	}
}
0