結果
問題 | No.828 全方位神童数 |
ユーザー | kopricky |
提出日時 | 2019-04-11 22:03:05 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 1,504 ms / 2,000 ms |
コード長 | 1,801 bytes |
コンパイル時間 | 176 ms |
コンパイル使用メモリ | 12,672 KB |
実行使用メモリ | 75,752 KB |
最終ジャッジ日時 | 2024-07-19 19:03:14 |
合計ジャッジ時間 | 33,243 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 32 ms
10,624 KB |
testcase_01 | AC | 29 ms
10,880 KB |
testcase_02 | AC | 29 ms
10,880 KB |
testcase_03 | AC | 80 ms
14,080 KB |
testcase_04 | AC | 43 ms
11,648 KB |
testcase_05 | AC | 78 ms
13,696 KB |
testcase_06 | AC | 40 ms
11,392 KB |
testcase_07 | AC | 38 ms
11,136 KB |
testcase_08 | AC | 42 ms
11,264 KB |
testcase_09 | AC | 35 ms
11,008 KB |
testcase_10 | AC | 66 ms
13,056 KB |
testcase_11 | AC | 35 ms
10,880 KB |
testcase_12 | AC | 68 ms
13,056 KB |
testcase_13 | AC | 475 ms
36,012 KB |
testcase_14 | AC | 1,140 ms
62,864 KB |
testcase_15 | AC | 387 ms
29,308 KB |
testcase_16 | AC | 542 ms
37,256 KB |
testcase_17 | AC | 412 ms
31,676 KB |
testcase_18 | AC | 781 ms
47,876 KB |
testcase_19 | AC | 1,385 ms
74,780 KB |
testcase_20 | AC | 927 ms
54,556 KB |
testcase_21 | AC | 1,203 ms
66,660 KB |
testcase_22 | AC | 276 ms
24,168 KB |
testcase_23 | AC | 96 ms
14,848 KB |
testcase_24 | AC | 725 ms
44,092 KB |
testcase_25 | AC | 276 ms
24,536 KB |
testcase_26 | AC | 1,363 ms
70,248 KB |
testcase_27 | AC | 1,133 ms
61,772 KB |
testcase_28 | AC | 1,365 ms
70,692 KB |
testcase_29 | AC | 1,294 ms
67,408 KB |
testcase_30 | AC | 706 ms
43,896 KB |
testcase_31 | AC | 675 ms
43,044 KB |
testcase_32 | AC | 1,318 ms
70,272 KB |
testcase_33 | AC | 1,494 ms
75,456 KB |
testcase_34 | AC | 1,178 ms
62,508 KB |
testcase_35 | AC | 758 ms
45,836 KB |
testcase_36 | AC | 954 ms
54,056 KB |
testcase_37 | AC | 606 ms
39,532 KB |
testcase_38 | AC | 866 ms
50,068 KB |
testcase_39 | AC | 1,327 ms
70,716 KB |
testcase_40 | AC | 617 ms
39,400 KB |
testcase_41 | AC | 499 ms
34,716 KB |
testcase_42 | AC | 1,504 ms
75,752 KB |
testcase_43 | AC | 831 ms
70,596 KB |
testcase_44 | AC | 318 ms
34,012 KB |
testcase_45 | AC | 482 ms
45,884 KB |
ソースコード
import sys import time 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): 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 return ver1 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): pp = i for v in G[i]: par = uf.find(v) graph[i].append(ver[par]) pp = uf.unite(par, pp) ver[uf.find(i)] = i dfs(n-1) ret = 1 for i in range(n): ret = ret * ans[i] % MOD print(ret)