結果
問題 | No.2494 Sum within Components |
ユーザー | mkawa2 |
提出日時 | 2023-10-06 21:35:04 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 272 ms / 2,000 ms |
コード長 | 1,845 bytes |
コンパイル時間 | 227 ms |
コンパイル使用メモリ | 82,432 KB |
実行使用メモリ | 102,780 KB |
最終ジャッジ日時 | 2024-07-26 15:50:32 |
合計ジャッジ時間 | 3,377 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 41 ms
52,352 KB |
testcase_01 | AC | 38 ms
52,480 KB |
testcase_02 | AC | 39 ms
52,224 KB |
testcase_03 | AC | 39 ms
52,480 KB |
testcase_04 | AC | 40 ms
52,352 KB |
testcase_05 | AC | 39 ms
52,480 KB |
testcase_06 | AC | 38 ms
52,608 KB |
testcase_07 | AC | 38 ms
52,864 KB |
testcase_08 | AC | 39 ms
52,992 KB |
testcase_09 | AC | 94 ms
76,416 KB |
testcase_10 | AC | 104 ms
77,780 KB |
testcase_11 | AC | 80 ms
76,644 KB |
testcase_12 | AC | 112 ms
77,440 KB |
testcase_13 | AC | 109 ms
77,056 KB |
testcase_14 | AC | 233 ms
97,408 KB |
testcase_15 | AC | 230 ms
90,996 KB |
testcase_16 | AC | 147 ms
94,808 KB |
testcase_17 | AC | 107 ms
102,532 KB |
testcase_18 | AC | 141 ms
102,780 KB |
testcase_19 | AC | 272 ms
101,424 KB |
ソースコード
import sys # sys.setrecursionlimit(200005) # sys.set_int_max_str_digits(200005) int1 = lambda x: int(x)-1 pDB = lambda *x: print(*x, end="\n", file=sys.stderr) p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] # dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] # inf = -1-(-1 << 31) inf = -1-(-1 << 63) # md = 10**9+7 md = 998244353 class UnionFind: def __init__(self, n, aa): self._tree = [-1]*n self._aa = aa # number of connected component self.cnt = n def root(self, u): stack = [] while self._tree[u] >= 0: stack.append(u) u = self._tree[u] for v in stack: self._tree[v] = u return u def same(self, u, v): return self.root(u) == self.root(v) def merge(self, u, v): u, v = self.root(u), self.root(v) if u == v: return False if self._tree[u] > self._tree[v]: u, v = v, u self._tree[u] += self._tree[v] self._tree[v] = u self._aa[u] += self._aa[v] self.cnt -= 1 return True # size of connected component def size(self, u): return -self._tree[self.root(u)] n, m = LI() aa = LI() uf=UnionFind(n,aa) for _ in range(m): u,v=LI1() uf.merge(u,v) ans=1 fin=[0]*n for u in range(n): u=uf.root(u) if fin[u]:continue fin[u]=1 s=uf._aa[u] c=uf.size(u) ans*=pow(s,c,md) ans%=md print(ans)