Submission #8424355


Source Code Expand

import sys
input = sys.stdin.readline


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 same(self, x, y):
        return self.find(x) == self.find(y)


def main():
    N, M = map(int, input().split())
    P = list(map(int, input().split()))
    uf = UnionFind(N)
    for i in range(M):
        x, y = map(int, input().split())
        uf.union(x-1, y-1)
    ans = 0
    for i in range(N):
        if uf.same(P[i]-1, i):
            ans += 1
    print(ans)


if __name__ == '__main__':
    main()

Submission Info

Submission Time
Task D - Equals
User takeuchi839
Language PyPy3 (2.4.0)
Score 400
Code Size 1040 Byte
Status AC
Exec Time 663 ms
Memory 84368 KB

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 163 ms 38256 KB
0_001.txt AC 162 ms 38384 KB
0_002.txt AC 160 ms 38256 KB
0_003.txt AC 161 ms 38256 KB
1_004.txt AC 262 ms 41816 KB
1_005.txt AC 315 ms 53648 KB
1_006.txt AC 663 ms 84368 KB
1_007.txt AC 161 ms 38256 KB
1_008.txt AC 161 ms 38256 KB
1_009.txt AC 161 ms 38256 KB
1_010.txt AC 160 ms 38256 KB
1_011.txt AC 160 ms 38256 KB
1_012.txt AC 161 ms 38256 KB
1_013.txt AC 168 ms 38640 KB
1_014.txt AC 183 ms 40176 KB
1_015.txt AC 164 ms 38384 KB
1_016.txt AC 167 ms 38384 KB
1_017.txt AC 177 ms 39152 KB
1_018.txt AC 274 ms 42984 KB
1_019.txt AC 208 ms 52752 KB
1_020.txt AC 208 ms 52880 KB
1_021.txt AC 221 ms 52880 KB
1_022.txt AC 636 ms 82448 KB