Submission #8392573


Source Code Expand

N, M = map(int, input().split())

p = list(map(int, input().split()))

class UnionFind():
    def __init__(self, n):
        self.par = [i for i in range(n)]
        self.rank = [0 for i in range(n)]

    def find(self, x):
        if self.par[x] == x:
            return x
        self.par[x] = self.find(self.par[x])
        return self.par[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        elif self.rank[y] < self.rank[x]:
            self.par[y] = x
        else:
            self.rank[y] += 1
            self.par[x] = y
        

uf = UnionFind(N)

for i in range(M):
    x, y = map(int, input().split())
    uf.unite(x-1, y-1)

ans = 0
for i in range(N):
    if uf.find(p[i]-1) == uf.find(i):
        ans += 1

print(ans)

Submission Info

Submission Time
Task D - Equals
User b1u3
Language Python (3.4.3)
Score 400
Code Size 914 Byte
Status AC
Exec Time 669 ms
Memory 14008 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 17 ms 3064 KB
0_001.txt AC 17 ms 3064 KB
0_002.txt AC 17 ms 3064 KB
0_003.txt AC 17 ms 3064 KB
1_004.txt AC 431 ms 3064 KB
1_005.txt AC 523 ms 13812 KB
1_006.txt AC 669 ms 13812 KB
1_007.txt AC 17 ms 3064 KB
1_008.txt AC 17 ms 3064 KB
1_009.txt AC 17 ms 3064 KB
1_010.txt AC 17 ms 3064 KB
1_011.txt AC 17 ms 3064 KB
1_012.txt AC 18 ms 3064 KB
1_013.txt AC 21 ms 3064 KB
1_014.txt AC 38 ms 3064 KB
1_015.txt AC 18 ms 3064 KB
1_016.txt AC 19 ms 3064 KB
1_017.txt AC 23 ms 3064 KB
1_018.txt AC 389 ms 3064 KB
1_019.txt AC 114 ms 14008 KB
1_020.txt AC 102 ms 13812 KB
1_021.txt AC 108 ms 13812 KB
1_022.txt AC 630 ms 13812 KB