結果
問題 | No.872 All Tree Path |
ユーザー | None |
提出日時 | 2021-03-12 17:40:19 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 793 ms / 3,000 ms |
コード長 | 2,034 bytes |
コンパイル時間 | 241 ms |
コンパイル使用メモリ | 82,076 KB |
実行使用メモリ | 156,660 KB |
最終ジャッジ日時 | 2024-10-14 04:58:53 |
合計ジャッジ時間 | 9,357 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 793 ms
151,016 KB |
testcase_01 | AC | 767 ms
152,284 KB |
testcase_02 | AC | 768 ms
149,752 KB |
testcase_03 | AC | 269 ms
154,832 KB |
testcase_04 | AC | 36 ms
52,492 KB |
testcase_05 | AC | 790 ms
156,660 KB |
testcase_06 | AC | 766 ms
149,252 KB |
testcase_07 | AC | 782 ms
151,984 KB |
testcase_08 | AC | 113 ms
81,332 KB |
testcase_09 | AC | 117 ms
81,280 KB |
testcase_10 | AC | 114 ms
81,024 KB |
testcase_11 | AC | 124 ms
81,460 KB |
testcase_12 | AC | 116 ms
81,152 KB |
testcase_13 | AC | 36 ms
52,096 KB |
testcase_14 | AC | 37 ms
52,480 KB |
testcase_15 | AC | 39 ms
52,480 KB |
testcase_16 | AC | 36 ms
51,968 KB |
testcase_17 | AC | 37 ms
52,480 KB |
testcase_18 | AC | 37 ms
52,480 KB |
testcase_19 | AC | 37 ms
52,352 KB |
ソースコード
class Tree(): def __init__(self, n, decrement=1): self.n = n self.edges = [[] for _ in range(n)] self._edge_label = [[] for _ in range(n)] self.root = None self.size = [1]*n # number of nodes in subtree self.decrement = decrement def add_edge(self, u, v, i): u, v = u-self.decrement, v-self.decrement self.edges[u].append(v) self.edges[v].append(u) self._edge_label[u].append((v,i)) self._edge_label[v].append((u,i)) def add_edges(self, edges): for i, p in enumerate(edges): u, v = p u, v = u-self.decrement, v-self.decrement self.edges[u].append(v) self.edges[v].append(u) self._edge_label[u].append((v, i)) self._edge_label[v].append((u, i)) def set_root(self, root): root -= self.decrement self.depth = [-1]*self.n self.root = root self.par = [-1]*self.n self.depth[root] = 0 self.edge_label = [-1]*self.n # labeled by child self.order = [root] next_set = [root] while next_set: p = next_set.pop() for q, i in self._edge_label[p]: if self.depth[q] != -1: continue self.par[q] = p self.depth[q] = self.depth[p]+1 self.edge_label[q]=i self.order.append(q) next_set.append(q) for p in self.order[::-1]: for q in self.edges[p]: if self.par[p] == q: continue self.size[p] += self.size[q] ######################################################################################################### import sys input = sys.stdin.readline N= int(input()) T = Tree(N,decrement=1) W=[] for i in range(N-1): x, y, w = map(int, input().split()) T.add_edge(x,y,i) W.append(w) T.set_root(1) S=T.size[0] res=0 for i in range(1,N): s=T.size[i] res+=s*(S-s)*W[T.edge_label[i]] print(res*2)