Submission #3966231


Source Code Expand

N, M = [int(_) for _ in input().split()]
P = [0] + [int(_) for _ in input().split()]
XY = [[int(_) for _ in input().split()] for _ in range(M)]

UF = list(range(N + 1))


def find(x):
    if UF[x] != x:
        UF[x] = find(UF[x])
    return UF[x]


def unite(x, y):
    UF[find(x)] = min(find(x), find(y))
    UF[find(y)] = UF[x]


def same(x, y):
    return find(x) == find(y)


for x, y in XY:
    unite(x, y)

ans = 0
for i in range(1, N + 1):
    if same(i, P[i]):
        ans += 1
print(ans)

Submission Info

Submission Time
Task D - Equals
User yydoco
Language Python (3.4.3)
Score 0
Code Size 528 Byte
Status RE
Exec Time 411 ms
Memory 30212 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 400
Status
AC × 4
AC × 12
RE × 11
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 18 ms 3064 KB
0_001.txt AC 18 ms 3064 KB
0_002.txt AC 18 ms 3064 KB
0_003.txt AC 18 ms 3064 KB
1_004.txt RE 370 ms 18384 KB
1_005.txt RE 406 ms 30188 KB
1_006.txt RE 411 ms 30212 KB
1_007.txt AC 18 ms 3064 KB
1_008.txt AC 18 ms 3064 KB
1_009.txt AC 18 ms 3064 KB
1_010.txt AC 18 ms 3064 KB
1_011.txt AC 18 ms 3064 KB
1_012.txt RE 77 ms 3872 KB
1_013.txt RE 81 ms 3984 KB
1_014.txt RE 90 ms 4464 KB
1_015.txt AC 19 ms 3064 KB
1_016.txt RE 78 ms 3968 KB
1_017.txt RE 80 ms 4116 KB
1_018.txt RE 345 ms 18936 KB
1_019.txt AC 100 ms 13812 KB
1_020.txt AC 105 ms 13812 KB
1_021.txt RE 118 ms 13812 KB
1_022.txt RE 404 ms 30212 KB