結果
問題 | No.872 All Tree Path |
ユーザー | toyuzuko |
提出日時 | 2020-05-06 16:35:01 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 1,426 ms / 3,000 ms |
コード長 | 1,672 bytes |
コンパイル時間 | 135 ms |
コンパイル使用メモリ | 12,928 KB |
実行使用メモリ | 125,184 KB |
最終ジャッジ日時 | 2024-06-28 18:14:04 |
合計ジャッジ時間 | 13,490 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,426 ms
114,048 KB |
testcase_01 | AC | 1,403 ms
113,280 KB |
testcase_02 | AC | 1,381 ms
116,992 KB |
testcase_03 | AC | 857 ms
125,184 KB |
testcase_04 | AC | 26 ms
10,752 KB |
testcase_05 | AC | 1,415 ms
113,280 KB |
testcase_06 | AC | 1,391 ms
115,712 KB |
testcase_07 | AC | 1,371 ms
113,024 KB |
testcase_08 | AC | 116 ms
21,120 KB |
testcase_09 | AC | 115 ms
21,120 KB |
testcase_10 | AC | 110 ms
20,992 KB |
testcase_11 | AC | 115 ms
21,120 KB |
testcase_12 | AC | 111 ms
21,120 KB |
testcase_13 | AC | 26 ms
10,624 KB |
testcase_14 | AC | 25 ms
10,624 KB |
testcase_15 | AC | 26 ms
10,752 KB |
testcase_16 | AC | 26 ms
10,752 KB |
testcase_17 | AC | 26 ms
10,752 KB |
testcase_18 | AC | 26 ms
10,752 KB |
testcase_19 | AC | 25 ms
10,752 KB |
ソースコード
class Tree(): def __init__(self, n, edge): self.n = n self.tree = [[] for _ in range(n)] for e in edge: self.tree[e[0] - 1].append((e[1] - 1, e[2])) self.tree[e[1] - 1].append((e[0] - 1, e[2])) def setroot(self, root): self.root = root self.parent = [None for _ in range(self.n)] self.parent[root] = -1 self.depth = [None for _ in range(self.n)] self.depth[root] = 0 self.distance = [None for _ in range(self.n)] self.distance[root] = 0 self.order = [] self.order.append(root) self.cost = [0 for _ in range(self.n)] self.size = [1 for _ in range(self.n)] stack = [root] while stack: node = stack.pop() for adj, cost in self.tree[node]: if self.parent[adj] is None: self.parent[adj] = node self.depth[adj] = self.depth[node] + 1 self.distance[adj] = self.distance[node] + cost self.cost[adj] = cost self.order.append(adj) stack.append(adj) for node in self.order[::-1]: for adj, cost in self.tree[node]: if self.parent[node] == adj: continue self.size[node] += self.size[adj] import sys input = sys.stdin.readline N = int(input()) E = [tuple(map(int, input().split())) for _ in range(N - 1)] tree = Tree(N, E) tree.setroot(0) res = 0 for node in range(N): if node == 0: continue res += tree.cost[node] * tree.size[node] * (N - tree.size[node]) * 2 print(res)