結果
問題 | No.828 全方位神童数 |
ユーザー | kopricky |
提出日時 | 2019-04-11 20:42:21 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 1,448 ms / 2,000 ms |
コード長 | 1,811 bytes |
コンパイル時間 | 106 ms |
コンパイル使用メモリ | 12,928 KB |
実行使用メモリ | 74,876 KB |
最終ジャッジ日時 | 2024-07-19 08:12:45 |
合計ジャッジ時間 | 29,493 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 31 ms
10,752 KB |
testcase_01 | AC | 30 ms
10,880 KB |
testcase_02 | AC | 31 ms
10,880 KB |
testcase_03 | AC | 88 ms
14,080 KB |
testcase_04 | AC | 45 ms
11,648 KB |
testcase_05 | AC | 75 ms
13,824 KB |
testcase_06 | AC | 37 ms
11,392 KB |
testcase_07 | AC | 37 ms
11,392 KB |
testcase_08 | AC | 40 ms
11,392 KB |
testcase_09 | AC | 32 ms
11,008 KB |
testcase_10 | AC | 60 ms
12,928 KB |
testcase_11 | AC | 34 ms
11,008 KB |
testcase_12 | AC | 64 ms
13,056 KB |
testcase_13 | AC | 468 ms
35,628 KB |
testcase_14 | AC | 1,056 ms
61,720 KB |
testcase_15 | AC | 350 ms
29,036 KB |
testcase_16 | AC | 486 ms
36,520 KB |
testcase_17 | AC | 404 ms
31,296 KB |
testcase_18 | AC | 724 ms
47,240 KB |
testcase_19 | AC | 1,362 ms
73,496 KB |
testcase_20 | AC | 887 ms
53,836 KB |
testcase_21 | AC | 1,151 ms
65,768 KB |
testcase_22 | AC | 264 ms
24,040 KB |
testcase_23 | AC | 91 ms
14,720 KB |
testcase_24 | AC | 673 ms
43,576 KB |
testcase_25 | AC | 252 ms
24,288 KB |
testcase_26 | AC | 1,299 ms
69,356 KB |
testcase_27 | AC | 1,113 ms
61,048 KB |
testcase_28 | AC | 1,323 ms
69,672 KB |
testcase_29 | AC | 1,222 ms
66,508 KB |
testcase_30 | AC | 668 ms
43,428 KB |
testcase_31 | AC | 651 ms
42,408 KB |
testcase_32 | AC | 1,283 ms
69,248 KB |
testcase_33 | AC | 1,432 ms
74,420 KB |
testcase_34 | AC | 1,105 ms
61,600 KB |
testcase_35 | AC | 698 ms
45,472 KB |
testcase_36 | AC | 887 ms
53,412 KB |
testcase_37 | AC | 574 ms
39,192 KB |
testcase_38 | AC | 798 ms
49,448 KB |
testcase_39 | AC | 1,308 ms
69,564 KB |
testcase_40 | AC | 542 ms
38,972 KB |
testcase_41 | AC | 462 ms
34,364 KB |
testcase_42 | AC | 1,448 ms
74,876 KB |
testcase_43 | AC | 809 ms
70,708 KB |
testcase_44 | AC | 325 ms
34,140 KB |
testcase_45 | AC | 476 ms
45,760 KB |
ソースコード
import sys MOD = 1000000007 class UnionFind: def __init__(self, node_size): self._node = node_size self.par = [i for i in range(self._node)] self.rank = [0] * self._node def find(self, ver): if self.par[ver] == ver: return ver else: self.par[ver] = self.find(self.par[ver]) return self.par[ver] def unite(self, ver1, ver2): ver1, ver2 = self.find(ver1), self.find(ver2) if ver1 == ver2: return if self.rank[ver1] < self.rank[ver2]: ver1, ver2 = ver2, ver1 self.par[ver2] = ver1 if self.rank[ver1] == self.rank[ver2]: self.rank[ver1] += 1 def same(self, ver1, ver2): return self.find(ver1) == self.find(ver2) def dfs(u): head = 1 stack[0], stack[1] = -1, u while head > 0: cur, prv, depth = stack[head], stack[head-1], head if visit[cur] == 0: ans[cur] += depth if visit[cur] == len(graph[cur]): head -= 1 continue head += 1 stack[head] = graph[cur][visit[cur]] visit[cur] += 1 if __name__ == '__main__': n = int(sys.stdin.readline()) G, graph = [[] for _ in range(n)], [[] for _ in range(n)] ver, visit, stack = [0]*n, [0]*n, [0]*(n+1) ans = list(map(int, sys.stdin.readline().split())) for i in range(n-1): u, v = map(int, sys.stdin.readline().split()) if u > v: u, v = v, u G[v-1].append(u-1) ver[i+1] = i+1 uf = UnionFind(n) for i in range(n): for v in G[i]: graph[i].append(ver[uf.find(v)]), uf.unite(i, v) ver[uf.find(i)] = i dfs(n-1) ret = 1 for i in range(n): ret = ret * ans[i] % MOD print(ret)