Submission #23513430


Source Code Expand

import collections
class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self.has_cycles = [0] * n
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            # 積極的aggregation
            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:
            self.has_cycles[x] = 1
            return
        # 既知の親子で小さいものが左に来るべき
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        # rootノードを負の値で参照量をカウントしたいため、このような+=が入っている
        self.parents[x] += self.parents[y]
        # rootノードでなければ、正のindex値を入れる
        self.parents[y] = x
    def size(self, x):
        # rootノードの参照料を保存したものを取り出している
        return -self.parents[self.find(x)]
    def same(self, x, y):
        # 同じルートを持つか
        return self.find(x) == self.find(y)
    def roots(self):
        # どのノードがrootとなるか
        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) -> "Tuple[GroupMember, GroupCycle]":
        # rootをkeyに子をvalueのlistに, 閉路情報も返す
        group_members = collections.defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        group_cycle = collections.defaultdict(bool)
        for group, members in group_members.items():
            group_cycle[group] = True if any([self.has_cycles[member] for member in members]) else False
        return group_members, group_cycle
N,M=map(int,input().split())
*P,=map(int,input().split())

uf = UnionFind(n=N)
for _ in range(M):
    x,y = map(int,input().split())
    x-=1; y-=1
    uf.union(x,y)

gm, gc = uf.all_group_members()

ans = 0
for root, mmb in gm.items():
    vals = [P[mi]-1 for mi in mmb]
    ans += len(set(vals) & set(mmb))
print(ans)

Submission Info

Submission Time
Task D - Equals
User lightning
Language PyPy3 (7.3.0)
Score 400
Code Size 2426 Byte
Status AC
Exec Time 396 ms
Memory 127920 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 4
AC × 23
Set Name Test Cases
Sample 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt
All 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt, 1_004.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt, 1_015.txt, 1_016.txt, 1_017.txt, 1_018.txt, 1_019.txt, 1_020.txt, 1_021.txt, 1_022.txt
Case Name Status Exec Time Memory
0_000.txt AC 79 ms 64964 KiB
0_001.txt AC 58 ms 65480 KiB
0_002.txt AC 60 ms 65028 KiB
0_003.txt AC 58 ms 65048 KiB
1_004.txt AC 191 ms 69564 KiB
1_005.txt AC 295 ms 106380 KiB
1_006.txt AC 396 ms 127920 KiB
1_007.txt AC 60 ms 65296 KiB
1_008.txt AC 63 ms 65468 KiB
1_009.txt AC 60 ms 65156 KiB
1_010.txt AC 60 ms 65320 KiB
1_011.txt AC 63 ms 67012 KiB
1_012.txt AC 65 ms 67020 KiB
1_013.txt AC 77 ms 68136 KiB
1_014.txt AC 97 ms 69360 KiB
1_015.txt AC 68 ms 67160 KiB
1_016.txt AC 68 ms 67324 KiB
1_017.txt AC 82 ms 68344 KiB
1_018.txt AC 182 ms 69432 KiB
1_019.txt AC 155 ms 103648 KiB
1_020.txt AC 154 ms 104492 KiB
1_021.txt AC 179 ms 105820 KiB
1_022.txt AC 395 ms 114504 KiB