結果

問題 No.1098 LCAs
ユーザー ks2mks2m
提出日時 2020-06-26 22:20:52
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,222 bytes
コンパイル時間 2,404 ms
コンパイル使用メモリ 74,768 KB
実行使用メモリ 110,372 KB
最終ジャッジ日時 2023-09-18 05:40:24
合計ジャッジ時間 19,669 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
49,592 KB
testcase_01 WA -
testcase_02 AC 40 ms
49,332 KB
testcase_03 AC 41 ms
49,988 KB
testcase_04 WA -
testcase_05 AC 40 ms
49,844 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 830 ms
94,928 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 TLE -
testcase_29 TLE -
testcase_30 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class Main {
	static int n;
	static List<List<Integer>> list;
	static Obj[] ans;

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] sa = br.readLine().split(" ");
		n = Integer.parseInt(sa[0]);
		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();

		ans = new Obj[n];
		dfs(0, -1);
		PrintWriter pw = new PrintWriter(System.out);
		for (Obj o : ans) {
			pw.println(o.val);
		}
		pw.flush();
	}

	static Obj dfs(int x, int p) {
		Obj o = new Obj();
		long val = 0;
		for (int c : list.get(x)) {
			if (c != p) {
				Obj res = dfs(c, x);
				o.size += res.size;
				val += res.val;
			}
		}
		o.size++;
		o.val = (long) o.size * o.size - val;
		return ans[x] = o;
	}

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