結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー ks2mks2m
提出日時 2021-03-05 23:30:08
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,197 ms / 2,000 ms
コード長 5,101 bytes
コンパイル時間 3,008 ms
コンパイル使用メモリ 91,208 KB
実行使用メモリ 97,060 KB
最終ジャッジ日時 2024-04-16 11:36:55
合計ジャッジ時間 24,914 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
37,100 KB
testcase_01 AC 60 ms
37,316 KB
testcase_02 AC 59 ms
37,256 KB
testcase_03 AC 1,077 ms
90,124 KB
testcase_04 AC 1,096 ms
90,708 KB
testcase_05 AC 1,124 ms
90,412 KB
testcase_06 AC 1,197 ms
92,552 KB
testcase_07 AC 1,100 ms
91,764 KB
testcase_08 AC 906 ms
77,028 KB
testcase_09 AC 524 ms
59,580 KB
testcase_10 AC 547 ms
61,240 KB
testcase_11 AC 332 ms
52,228 KB
testcase_12 AC 721 ms
70,544 KB
testcase_13 AC 771 ms
74,476 KB
testcase_14 AC 896 ms
77,116 KB
testcase_15 AC 683 ms
67,096 KB
testcase_16 AC 196 ms
45,244 KB
testcase_17 AC 207 ms
45,416 KB
testcase_18 AC 968 ms
87,620 KB
testcase_19 AC 317 ms
51,400 KB
testcase_20 AC 157 ms
42,584 KB
testcase_21 AC 700 ms
66,124 KB
testcase_22 AC 739 ms
68,864 KB
testcase_23 AC 172 ms
44,572 KB
testcase_24 AC 174 ms
44,568 KB
testcase_25 AC 143 ms
41,660 KB
testcase_26 AC 170 ms
43,412 KB
testcase_27 AC 88 ms
38,780 KB
testcase_28 AC 106 ms
39,452 KB
testcase_29 AC 187 ms
44,992 KB
testcase_30 AC 175 ms
44,852 KB
testcase_31 AC 131 ms
40,860 KB
testcase_32 AC 175 ms
42,340 KB
testcase_33 AC 331 ms
53,384 KB
testcase_34 AC 817 ms
89,192 KB
testcase_35 AC 482 ms
64,976 KB
testcase_36 AC 262 ms
49,368 KB
testcase_37 AC 1,109 ms
97,060 KB
testcase_38 AC 1,121 ms
89,640 KB
testcase_39 AC 62 ms
37,412 KB
testcase_40 AC 61 ms
37,104 KB
testcase_41 AC 61 ms
37,344 KB
testcase_42 AC 61 ms
37,288 KB
testcase_43 AC 61 ms
37,172 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;
		long 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