Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque
import copy


def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, M] = [int(input_raw[0]),int(input_raw[1])]
    p_list = []
    input_raw = read_func().strip().split()
    for i in range(N):
        p_list.append(int(input_raw[i]))
    swap_list = {}
    for i in range(M):
        input_raw = read_func().strip().split()
        [x, y] =  [int(input_raw[0]),int(input_raw[1])]
        if x not in swap_list:
            swap_list[x] = [y]
        else:
            swap_list[x].append(y)
        if y not in swap_list:
            swap_list[y] = [x]
        else:
            swap_list[y].append(x)

    done_list = [False for i in range(N + 1)]
    done_list[0] = True

    adj_set_list = [set([]) for i in range(N + 1)]
    
    for i in range(1, N + 1):

        if done_list[i] == True:
            continue
        nd_list = deque([i])
        while 1:
            if len(nd_list) == 0:
                break
            nd = nd_list.popleft()
            done_list[nd] = True
            adj_set_list[i].add(nd)
            adj_set_list[nd] = adj_set_list[i]
            if nd in swap_list:
                for adj in swap_list[nd]:
                    if done_list[adj] == True:
                        continue
                    nd_list.append(adj)
    count = 0
    for i in range(len(p_list)):
        if p_list[i] in adj_set_list[i + 1]:
            count += 1
    print count




if __name__ == '__main__':
    main()

Fixed submission:
import sys
from collections import deque
import copy


def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, M] = [int(input_raw[0]),int(input_raw[1])]
    p_list = []
    input_raw = read_func().strip().split()
    for i in range(N):
        p_list.append(int(input_raw[i]))
    swap_list = {}
    for i in range(M):
        input_raw = read_func().strip().split()
        [x, y] =  [int(input_raw[0]),int(input_raw[1])]
        if x not in swap_list:
            swap_list[x] = [y]
        else:
            swap_list[x].append(y)
        if y not in swap_list:
            swap_list[y] = [x]
        else:
            swap_list[y].append(x)

    done_list = [False for i in range(N + 1)]
    done_list[0] = True

    adj_set_list = [set([]) for i in range(N + 1)]
    
    for i in range(1, N + 1):

        if done_list[i] == True:
            continue
        nd_list = deque([i])
        while 1:
            if len(nd_list) == 0:
                break
            nd = nd_list.popleft()
            #done_list[nd] = True
            adj_set_list[i].add(nd)
            adj_set_list[nd] = adj_set_list[i]
            if nd in swap_list:
                for adj in swap_list[nd]:
                    if done_list[adj] == True:
                        continue
                    done_list[adj] = True
                    nd_list.append(adj)
    count = 0
    for i in range(len(p_list)):
        if p_list[i] in adj_set_list[i + 1]:
            count += 1
    print count




if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque
import copy


def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, M] = [int(input_raw[0]),int(input_raw[1])]
    p_list = []
    input_raw = read_func().strip().split()
    for i in range(N):
        p_list.append(int(input_raw[i]))
    swap_list = {}
    for i in range(M):
        input_raw = read_func().strip().split()
        [x, y] =  [int(input_raw[0]),int(input_raw[1])]
        if x not in swap_list:
            swap_list[x] = [y]
        else:
            swap_list[x].append(y)
        if y not in swap_list:
            swap_list[y] = [x]
        else:
            swap_list[y].append(x)

    done_list = [False for i in range(N + 1)]
    done_list[0] = True

    adj_set_list = [set([]) for i in range(N + 1)]
    
    for i in range(1, N + 1):
        nd_list = deque([])
        if done_list[i] == True:
            continue
        nd_list.append(i)
        while 1:
            if len(nd_list) == 0:
                break
            nd = nd_list.popleft()
            done_list[nd] = True
            adj_set_list[i].add(nd)
            adj_set_list[nd] = adj_set_list[i]
            if nd in swap_list:
                for adj in swap_list[nd]:
                    if done_list[adj] == True:
                        continue
                    nd_list.append(adj)
    count = 0
    for i in range(len(p_list)):
        if p_list[i] in adj_set_list[i + 1]:
            count += 1
    print count




if __name__ == '__main__':
    main()


Fixed submission:
import sys
from collections import deque
import copy


def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, M] = [int(input_raw[0]),int(input_raw[1])]
    p_list = []
    input_raw = read_func().strip().split()
    for i in range(N):
        p_list.append(int(input_raw[i]))
    swap_list = {}
    for i in range(M):
        input_raw = read_func().strip().split()
        [x, y] =  [int(input_raw[0]),int(input_raw[1])]
        if x not in swap_list:
            swap_list[x] = [y]
        else:
            swap_list[x].append(y)
        if y not in swap_list:
            swap_list[y] = [x]
        else:
            swap_list[y].append(x)

    done_list = [False for i in range(N + 1)]
    done_list[0] = True

    adj_set_list = [set([]) for i in range(N + 1)]
    
    for i in range(1, N + 1):

        if done_list[i] == True:
            continue
        nd_list = deque([i])
        while 1:
            if len(nd_list) == 0:
                break
            nd = nd_list.popleft()
            #done_list[nd] = True
            adj_set_list[i].add(nd)
            adj_set_list[nd] = adj_set_list[i]
            if nd in swap_list:
                for adj in swap_list[nd]:
                    if done_list[adj] == True:
                        continue
                    done_list[adj] = True
                    nd_list.append(adj)
    count = 0
    for i in range(len(p_list)):
        if p_list[i] in adj_set_list[i + 1]:
            count += 1
    print count




if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque
import copy


def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, M] = [int(input_raw[0]),int(input_raw[1])]
    p_list = []
    input_raw = read_func().strip().split()
    for i in range(N):
        p_list.append(int(input_raw[i]))
    swap_list = {}
    for i in range(M):
        input_raw = read_func().strip().split()
        [x, y] =  [int(input_raw[0]),int(input_raw[1])]
        if x not in swap_list:
            swap_list[x] = [y]
        else:
            swap_list[x].append(y)
        if y not in swap_list:
            swap_list[y] = [x]
        else:
            swap_list[y].append(x)

    done_list = [False for i in range(N + 1)]

    adj_set_list = [set([]) for i in range(N + 1)]
    
    for i in range(1, N + 1):
        nd_list = deque([])
        if done_list[i] == True:
            continue
        nd_list.append(i)
        while 1:
            if len(nd_list) == 0:
                break
            nd = nd_list.popleft()
            done_list[nd] = True
            adj_set_list[i].add(nd)
            adj_set_list[nd] = adj_set_list[i]
            if nd in swap_list:
                for adj in swap_list[nd]:
                    if done_list[adj] == True:
                        continue
                    nd_list.append(adj)
    count = 0
    for i in range(len(p_list)):
        if p_list[i] in adj_set_list[i + 1]:
            count += 1
    print count




if __name__ == '__main__':
    main()

Fixed submission:
import sys
from collections import deque
import copy


def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, M] = [int(input_raw[0]),int(input_raw[1])]
    p_list = []
    input_raw = read_func().strip().split()
    for i in range(N):
        p_list.append(int(input_raw[i]))
    swap_list = {}
    for i in range(M):
        input_raw = read_func().strip().split()
        [x, y] =  [int(input_raw[0]),int(input_raw[1])]
        if x not in swap_list:
            swap_list[x] = [y]
        else:
            swap_list[x].append(y)
        if y not in swap_list:
            swap_list[y] = [x]
        else:
            swap_list[y].append(x)

    done_list = [False for i in range(N + 1)]
    done_list[0] = True

    adj_set_list = [set([]) for i in range(N + 1)]
    
    for i in range(1, N + 1):

        if done_list[i] == True:
            continue
        nd_list = deque([i])
        while 1:
            if len(nd_list) == 0:
                break
            nd = nd_list.popleft()
            #done_list[nd] = True
            adj_set_list[i].add(nd)
            adj_set_list[nd] = adj_set_list[i]
            if nd in swap_list:
                for adj in swap_list[nd]:
                    if done_list[adj] == True:
                        continue
                    done_list[adj] = True
                    nd_list.append(adj)
    count = 0
    for i in range(len(p_list)):
        if p_list[i] in adj_set_list[i + 1]:
            count += 1
    print count




if __name__ == '__main__':
    main()