Submission #2501438


Source Code Expand

import queue
N,M=map(int,input().split())
p=[int(i)-1 for i in input().split()]
x=[0 for i in range(M)]
y=[0 for i in range(M)]
edge=[set() for i in range(N)]
for i in range(M):
    x[i],y[i]=map(int,input().split())
    x[i]-=1;y[i]-=1
    edge[x[i]].add(y[i])
    edge[y[i]].add(x[i])
k=0
L=[0 for i in range(N)]
q=queue.Queue()
reached=[0 for i in range(N)]
for i in  range(N):
    if reached[i]==1:
        continue
    k+=1
    q.put(i)
    reached[i]=1
    L[i]=k
    while(not(q.empty())):
        r=q.get()
        for v in edge[r]:
            if not(reached[v]):
                q.put(v)
                reached[v]=1
                L[v]=k
ans=0
for i in range(N):
    if L[i]==L[p[i]]:
        ans+=1
print(ans)

Submission Info

Submission Time
Task D - Equals
User shakayami
Language Python (3.4.3)
Score 400
Code Size 757 Byte
Status AC
Exec Time 1288 ms
Memory 58592 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 70 ms 4208 KB
0_001.txt AC 26 ms 3952 KB
0_002.txt AC 46 ms 3952 KB
0_003.txt AC 26 ms 3952 KB
1_004.txt AC 413 ms 23272 KB
1_005.txt AC 1288 ms 58592 KB
1_006.txt AC 1180 ms 40928 KB
1_007.txt AC 26 ms 3952 KB
1_008.txt AC 26 ms 3952 KB
1_009.txt AC 26 ms 3952 KB
1_010.txt AC 26 ms 3952 KB
1_011.txt AC 26 ms 3952 KB
1_012.txt AC 27 ms 3952 KB
1_013.txt AC 30 ms 4080 KB
1_014.txt AC 45 ms 4848 KB
1_015.txt AC 34 ms 4208 KB
1_016.txt AC 34 ms 4208 KB
1_017.txt AC 36 ms 4208 KB
1_018.txt AC 375 ms 18308 KB
1_019.txt AC 868 ms 36192 KB
1_020.txt AC 841 ms 36192 KB
1_021.txt AC 844 ms 36192 KB
1_022.txt AC 1204 ms 41812 KB