結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー ks2mks2m
提出日時 2021-03-05 23:27:53
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 5,094 bytes
コンパイル時間 2,537 ms
コンパイル使用メモリ 89,672 KB
実行使用メモリ 98,648 KB
最終ジャッジ日時 2024-10-07 05:17:11
合計ジャッジ時間 23,553 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
37,200 KB
testcase_01 AC 56 ms
37,216 KB
testcase_02 AC 56 ms
37,176 KB
testcase_03 AC 1,041 ms
94,552 KB
testcase_04 AC 972 ms
91,604 KB
testcase_05 AC 1,038 ms
92,556 KB
testcase_06 AC 1,084 ms
93,736 KB
testcase_07 AC 1,051 ms
94,316 KB
testcase_08 AC 754 ms
78,560 KB
testcase_09 AC 415 ms
60,400 KB
testcase_10 AC 510 ms
62,648 KB
testcase_11 AC 292 ms
51,624 KB
testcase_12 AC 656 ms
70,604 KB
testcase_13 AC 671 ms
73,476 KB
testcase_14 AC 727 ms
74,784 KB
testcase_15 AC 583 ms
69,376 KB
testcase_16 AC 192 ms
45,164 KB
testcase_17 AC 186 ms
45,120 KB
testcase_18 AC 878 ms
84,952 KB
testcase_19 AC 267 ms
50,584 KB
testcase_20 AC 138 ms
42,104 KB
testcase_21 AC 536 ms
66,272 KB
testcase_22 AC 498 ms
64,192 KB
testcase_23 AC 162 ms
44,384 KB
testcase_24 AC 170 ms
44,512 KB
testcase_25 AC 137 ms
41,332 KB
testcase_26 AC 146 ms
42,860 KB
testcase_27 AC 82 ms
38,432 KB
testcase_28 AC 97 ms
39,496 KB
testcase_29 AC 172 ms
44,640 KB
testcase_30 AC 158 ms
44,620 KB
testcase_31 AC 126 ms
40,816 KB
testcase_32 AC 150 ms
42,284 KB
testcase_33 AC 354 ms
53,804 KB
testcase_34 WA -
testcase_35 AC 415 ms
63,932 KB
testcase_36 AC 240 ms
48,616 KB
testcase_37 AC 937 ms
95,840 KB
testcase_38 AC 901 ms
87,824 KB
testcase_39 AC 55 ms
36,832 KB
testcase_40 AC 55 ms
37,284 KB
testcase_41 AC 57 ms
37,180 KB
testcase_42 AC 55 ms
37,104 KB
testcase_43 AC 58 ms
36,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import java.util.function.BiFunction;

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

		Rerooting3<Obj> r = new Rerooting3<>(n, edges, new Obj(),
				(o, i) -> {
					Obj ret = new Obj();
					ret.s = o.s;
					ret.v = o.v;
					return ret;
				},
				(o1, o2) -> {
					Obj ret = new Obj();
					ret.s = o1.s + o2.s;
					ret.v = o1.v + o2.v;
					return ret;
				},
				(o, i) -> {
					Obj ret = new Obj();
					ret.s = o.s + 1;
					ret.v = o.v + ret.s;
					return ret;
				});
		long ans = 0;
		for (int i = 0; i < n; i++) {
			ans += r.query(i).v;
		}
		System.out.println(ans);
	}

	static class Obj {
		int s, v;
	}

	static class Rerooting3<T> {
		public int nodeCnt;
		private T identity;
		private BiFunction<T, Integer, T> beforeMerge;
		private BiFunction<T, T, T> merge;
		private BiFunction<T, Integer, T> afterMerge;
		private List<List<Integer>> adjacents;
		private List<List<Integer>> indexForAdjacent;
		private Object[][] dp;
		private Object[] res;

		/**
		 * 初期化
		 * @param nodeCnt 頂点数(≧1)
		 * @param edges 木構造の辺の配列  例:{{0,1},{0,2},{1,3}}
		 * @param identity 単位元
		 * @param beforeMerge 指定頂点(部分木の子)を計算する関数(何もしない場合以外戻り値は新規オブジェクト)
		 * @param merge 部分木のマージ関数(戻り値は新規オブジェクト)
		 * @param afterMerge 指定頂点(部分木の根)の分を計算する関数(何もしない場合以外戻り値は新規オブジェクト)
		 */
		public Rerooting3(int nodeCnt, int[][] edges, T identity, BiFunction<T, Integer, T> beforeMerge,
				BiFunction<T, T, T> merge, BiFunction<T, Integer, T> afterMerge) {
			this.nodeCnt = nodeCnt;
			this.identity = identity;
			this.beforeMerge = beforeMerge;
			this.merge = merge;
			this.afterMerge = afterMerge;

			adjacents = new ArrayList<>(nodeCnt);
			indexForAdjacent = new ArrayList<>(nodeCnt);
			for (int i = 0; i < nodeCnt; i++) {
				adjacents.add(new ArrayList<>());
				indexForAdjacent.add(new ArrayList<>());
			}
			for (int i = 0; i < edges.length; i++) {
				int[] edge = edges[i];
				indexForAdjacent.get(edge[0]).add(adjacents.get(edge[1]).size());
				indexForAdjacent.get(edge[1]).add(adjacents.get(edge[0]).size());
				adjacents.get(edge[0]).add(edge[1]);
				adjacents.get(edge[1]).add(edge[0]);
			}

			dp = new Object[nodeCnt][];
			res = new Object[nodeCnt];
			for (int i = 0; i < nodeCnt; i++) {
				dp[i] = new Object[adjacents.get(i).size()];
			}
			if (nodeCnt == 1) {
				res[0] = afterMerge.apply(identity, 0);
			} else {
				initialize();
			}
		}

		@SuppressWarnings("unchecked")
		public T query(int node) {
			return (T) res[node];
		}

		@SuppressWarnings("unchecked")
		private void initialize() {
			int[] parents = new int[nodeCnt];
			int[] order = new int[nodeCnt];

			// InitOrderedTree
			int index = 0;
			Deque<Integer> stack = new ArrayDeque<>();
			stack.addFirst(0);
			parents[0] = -1;
			while (!stack.isEmpty()) {
				int current = stack.pollFirst();
				order[index++] = current;
				for (int adjacent : adjacents.get(current)) {
					if (adjacent == parents[current]) continue;
					stack.addFirst(adjacent);
					parents[adjacent] = current;
				}
			}

			// FromLeaf
			for (int i = order.length - 1; i >= 1; i--) {
				int node = order[i];
				int parent = parents[node];
				T accum = identity;
				int parentIndex = -1;
				for (int j = 0; j < adjacents.get(node).size(); j++) {
					if (adjacents.get(node).get(j) == parent) {
						parentIndex = j;
						continue;
					}
					accum = merge.apply(accum, beforeMerge.apply((T) dp[node][j], adjacents.get(node).get(j)));
				}
				dp[parent][indexForAdjacent.get(node).get(parentIndex)] = afterMerge.apply(accum, node);
			}

			// ToLeaf
			for (int node : order) {
				T accum = identity;
				Object[] accumsFromTail = new Object[adjacents.get(node).size()];
				accumsFromTail[accumsFromTail.length - 1] = identity;
				for (int j = accumsFromTail.length - 1; j >= 1; j--) {
					accumsFromTail[j - 1] = merge.apply(
							(T) accumsFromTail[j],
							beforeMerge.apply((T) dp[node][j], adjacents.get(node).get(j)));
				}
				for (int j = 0; j < accumsFromTail.length; j++) {
					dp[adjacents.get(node).get(j)][indexForAdjacent.get(node).get(j)] =
							afterMerge.apply(merge.apply(accum, (T) accumsFromTail[j]), node);
					accum = merge.apply(accum, beforeMerge.apply((T) dp[node][j], adjacents.get(node).get(j)));
				}
				res[node] = afterMerge.apply(accum, node);
			}
		}
	}
}
0