N,Q = map(int,input().split()) P = list(map(int,input().split())) #=======Union-Find========== #頂点数に合わせて初期化 #問題に合わせて要変更 root = [0]*(N+1) UFsize = [1]*(N+1) UFdict = {} for i,v in enumerate(root): root[i] = i UFdict[i] = 1 #ある頂点の根を求めて返す #途中通過した頂点は同じ根につなぎ直す def find(x): rootlist = [] UFX = x UFflag = True while UFflag: if root[UFX] == UFX: UFflag = False break else: rootlist.append(UFX) UFX = root[UFX] if len(rootlist) >= 1: for jj in rootlist: root[jj] = UFX return UFX #ある頂点とある頂点をつなぐ #頂点の数字の若い方が優先される def union(x,y): xx = find(x) yy = find(y) if xx == yy: return else: if xx