Submission #3402035


Source Code Expand

def getN():
    return int(input())


def getNM():
    return map(int, input().split())


def getList():
    return list(map(int, input().split()))

class UnionFind():
    def __init__(self, N):
        self.par = [i for i in range(N+20)]

    def root(self, n):
        if self.par[n] == n:
            return n
        else:
            return self.root(self.par[n])

    def isgroup(self, a, b):
        if self.root(a) == self.root(b):
            return True
        else:
            return False

    def merge(self, m, n):
        rm = self.root(m)
        rn = self.root(n)
        if rm == rn:
            pass
        else:
            self.par[rn] = rm

N,M = getNM()
pos = getList()
uf = UnionFind(N)
for i in range(M):
    x, y = getNM()
    uf.merge(x,y)

ans = 0

for i, p in enumerate(pos):
    if uf.isgroup(i+1, p):
        #print(i+1, p)
        ans += 1
    else:
        pass

#roots = [uf.root(i) for i in range(N+1)]
print(ans)

Submission Info

Submission Time
Task D - Equals
User socha
Language Python (3.4.3)
Score 0
Code Size 1003 Byte
Status RE
Exec Time 2104 ms
Memory 13812 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 400
Status
AC × 4
AC × 19
TLE × 3
RE × 1
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 TLE 2103 ms 3064 KB
1_005.txt TLE 2104 ms 13812 KB
1_006.txt AC 979 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 24 ms 3064 KB
1_014.txt AC 57 ms 3064 KB
1_015.txt AC 18 ms 3064 KB
1_016.txt AC 18 ms 3064 KB
1_017.txt AC 56 ms 3316 KB
1_018.txt TLE 2103 ms 3188 KB
1_019.txt AC 109 ms 13808 KB
1_020.txt AC 108 ms 13812 KB
1_021.txt AC 114 ms 13812 KB
1_022.txt RE 577 ms 13812 KB