結果

問題 No.1098 LCAs
ユーザー ks2mks2m
提出日時 2020-06-26 22:26:33
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,258 bytes
コンパイル時間 2,817 ms
コンパイル使用メモリ 75,176 KB
実行使用メモリ 110,324 KB
最終ジャッジ日時 2023-09-18 05:50:11
合計ジャッジ時間 20,747 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,588 KB
testcase_01 AC 43 ms
49,396 KB
testcase_02 AC 43 ms
49,244 KB
testcase_03 AC 43 ms
49,648 KB
testcase_04 AC 44 ms
49,500 KB
testcase_05 AC 45 ms
49,504 KB
testcase_06 AC 44 ms
49,344 KB
testcase_07 AC 44 ms
49,444 KB
testcase_08 AC 42 ms
49,492 KB
testcase_09 AC 43 ms
49,620 KB
testcase_10 AC 43 ms
49,944 KB
testcase_11 AC 42 ms
49,492 KB
testcase_12 AC 43 ms
49,700 KB
testcase_13 AC 78 ms
50,452 KB
testcase_14 AC 78 ms
50,760 KB
testcase_15 AC 89 ms
51,964 KB
testcase_16 AC 91 ms
51,732 KB
testcase_17 AC 87 ms
51,612 KB
testcase_18 AC 978 ms
97,748 KB
testcase_19 AC 976 ms
99,476 KB
testcase_20 AC 927 ms
95,556 KB
testcase_21 AC 927 ms
95,752 KB
testcase_22 AC 937 ms
97,916 KB
testcase_23 AC 873 ms
97,152 KB
testcase_24 AC 867 ms
95,476 KB
testcase_25 AC 828 ms
96,732 KB
testcase_26 AC 938 ms
98,672 KB
testcase_27 AC 926 ms
98,488 KB
testcase_28 TLE -
testcase_29 TLE -
testcase_30 AC 1,190 ms
109,220 KB
権限があれば一括ダウンロードができます

ソースコード

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 total = 0;
		for (int c : list.get(x)) {
			if (c != p) {
				Obj res = dfs(c, x);
				o.size += res.size;
				total += res.total;
			}
		}
		o.size++;
		o.total = (long) o.size * o.size;
		o.val = o.total - total;
		return ans[x] = o;
	}

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