結果

問題 No.828 全方位神童数
ユーザー maspymaspy
提出日時 2020-04-16 16:41:14
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,195 ms / 2,000 ms
コード長 1,505 bytes
コンパイル時間 71 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 74,044 KB
最終ジャッジ日時 2024-04-09 19:50:19
合計ジャッジ時間 25,997 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
10,752 KB
testcase_01 AC 26 ms
10,880 KB
testcase_02 AC 30 ms
10,752 KB
testcase_03 AC 67 ms
14,592 KB
testcase_04 AC 40 ms
11,776 KB
testcase_05 AC 72 ms
13,824 KB
testcase_06 AC 37 ms
11,520 KB
testcase_07 AC 33 ms
11,392 KB
testcase_08 AC 34 ms
11,648 KB
testcase_09 AC 30 ms
11,136 KB
testcase_10 AC 54 ms
13,568 KB
testcase_11 AC 31 ms
11,136 KB
testcase_12 AC 58 ms
13,440 KB
testcase_13 AC 377 ms
33,644 KB
testcase_14 AC 831 ms
59,716 KB
testcase_15 AC 279 ms
28,552 KB
testcase_16 AC 390 ms
34,808 KB
testcase_17 AC 316 ms
29,892 KB
testcase_18 AC 574 ms
44,524 KB
testcase_19 AC 1,069 ms
68,768 KB
testcase_20 AC 698 ms
50,636 KB
testcase_21 AC 906 ms
61,484 KB
testcase_22 AC 198 ms
23,372 KB
testcase_23 AC 77 ms
15,744 KB
testcase_24 AC 536 ms
43,008 KB
testcase_25 AC 209 ms
24,040 KB
testcase_26 AC 1,034 ms
68,652 KB
testcase_27 AC 896 ms
60,520 KB
testcase_28 AC 1,116 ms
68,972 KB
testcase_29 AC 1,012 ms
65,924 KB
testcase_30 AC 576 ms
42,876 KB
testcase_31 AC 548 ms
42,088 KB
testcase_32 AC 1,060 ms
68,428 KB
testcase_33 AC 1,156 ms
73,364 KB
testcase_34 AC 893 ms
61,000 KB
testcase_35 AC 576 ms
44,804 KB
testcase_36 AC 727 ms
52,696 KB
testcase_37 AC 466 ms
38,712 KB
testcase_38 AC 660 ms
48,892 KB
testcase_39 AC 1,050 ms
68,944 KB
testcase_40 AC 449 ms
38,756 KB
testcase_41 AC 381 ms
33,916 KB
testcase_42 AC 1,195 ms
74,044 KB
testcase_43 AC 679 ms
65,468 KB
testcase_44 AC 266 ms
32,036 KB
testcase_45 AC 412 ms
42,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
MOD = 10 ** 9 + 7

N = int(readline())
R = tuple(map(int, readline().split()))


class UnionFind:
    def __init__(self, N):
        self.root = list(range(N))
        self.size = [1] * (N)
        self.largest = list(range(N))

    def find_root(self, x):
        root = self.root
        while root[x] != x:
            root[x] = root[root[x]]
            x = root[x]
        return x

    def merge(self, x, y):
        x = self.find_root(x)
        y = self.find_root(y)
        if x == y:
            return False
        sx, sy = self.size[x], self.size[y]
        v = max(self.largest[x], self.largest[y])
        if sx < sy:
            self.root[x] = y
            self.size[y] += sx
            self.largest[y] = v
        else:
            self.root[y] = x
            self.size[x] += sy
            self.largest[x] = v
        return True


graph = [[] for _ in range(N + 1)]
m = map(int, read().split())
for u, v in zip(m, m):
    graph[u].append(v)
    graph[v].append(u)

parent = [0] * (N + 1)
uf = UnionFind(N + 1)
merge = uf.merge
find = uf.find_root
for v in range(1, N + 1):
    child = set(uf.largest[find(w)] for w in graph[v] if w < v)
    for w in child:
        merge(v, w)
        parent[w] = v

S = [1] * (N + 1)
for i in range(N - 1, 0, -1):
    S[i] += S[parent[i]]

answer = 1
for r, s in zip(R, S[1:]):
    answer *= (r + s)
    answer %= MOD

print(answer)
0