提出 #22096987


ソースコード 拡げる

from collections import defaultdict
 
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
 
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
 
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
 
        if x == y:
            return
 
        if self.parents[x] > self.parents[y]:
            x, y = y, x
 
        self.parents[x] += self.parents[y]
        self.parents[y] = x
 
    def size(self, x):
        return -self.parents[self.find(x)]
 
    def same(self, x, y):
        return self.find(x) == self.find(y)
 
    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]
 
    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]
 
    def group_count(self):
        return len(self.roots())
 
    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members
 
    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())
 
 
N = int(input())
A = list(map(int, input().split())) 
 
uf = UnionFind(N)
for i in range(N):
    uf.union(i,A[i]-1)
 
 
print((2**uf.group_count()-1)%998244353)

提出情報

提出日時
問題 B - Special Subsets
ユーザ H20
言語 PyPy3 (7.3.0)
得点 400
コード長 1530 Byte
結果 AC
実行時間 232 ms
メモリ 97196 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 400 / 400
結果
AC × 3
AC × 25
セット名 テストケース
Sample s1.txt, s2.txt, s3.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, s1.txt, s2.txt, s3.txt
ケース名 結果 実行時間 メモリ
01.txt AC 78 ms 65164 KiB
02.txt AC 56 ms 64908 KiB
03.txt AC 58 ms 65096 KiB
04.txt AC 57 ms 65356 KiB
05.txt AC 56 ms 65164 KiB
06.txt AC 212 ms 91356 KiB
07.txt AC 205 ms 91236 KiB
08.txt AC 205 ms 91184 KiB
09.txt AC 58 ms 65360 KiB
10.txt AC 109 ms 97196 KiB
11.txt AC 232 ms 91368 KiB
12.txt AC 221 ms 91328 KiB
13.txt AC 226 ms 91384 KiB
14.txt AC 230 ms 91408 KiB
15.txt AC 230 ms 91388 KiB
16.txt AC 228 ms 91632 KiB
17.txt AC 161 ms 96268 KiB
18.txt AC 212 ms 91308 KiB
19.txt AC 228 ms 91292 KiB
20.txt AC 226 ms 91384 KiB
21.txt AC 105 ms 91232 KiB
22.txt AC 105 ms 91268 KiB
s1.txt AC 61 ms 65176 KiB
s2.txt AC 58 ms 65180 KiB
s3.txt AC 57 ms 65096 KiB