Submission #4029842
Source Code Expand
n,m = map(int,input().split()) p = list(map(int,input().split())) for i in range(n): p[i]-=1 #################################### #Union Find par = [] #親 rank = [] #木の深さ #初期化 for i in range(n): #par[i]:i rank[i]:0 par.append(i) rank.append(0) #木の根を求める def find(x,par): if par[x] == x: return x else: return find(par[x],par) #xとyの属する集合の併合 def unite(x,y,par,rank): x = find(x,par) y = find(y,par) if x != y: #xとyの属している集合が異なる時 if rank[x] < rank[y]: par[x] = y else: par[y] = x if rank[x]==rank[y]: rank[x] += 1 #xとyが同じ集合に属するかの判定 def same(x,y,par): return find(x,par) == find(y,par) ####################################### for i in range(m): x,y = map(int,input().split()) x -= 1 y -=1 unite(x,y,par,rank) a = [[] for i in range(n)] b = [[] for i in range(n)] for i in range(n): k = find(i,par) a[k].append(i) b[k].append(p[i]) res = 0 for i in range(n): if len(a[i])!= 0 and len(b[i])!=0: res += len(set(a[i])&set(b[i])) print(res)
Submission Info
Submission Time | |
---|---|
Task | D - Equals |
User | juppy |
Language | Python (3.4.3) |
Score | 400 |
Code Size | 1280 Byte |
Status | AC |
Exec Time | 716 ms |
Memory | 45536 KB |
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 400 / 400 | ||||
Status |
|
|
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 | AC | 408 ms | 3188 KB |
1_005.txt | AC | 694 ms | 38500 KB |
1_006.txt | AC | 716 ms | 45536 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 | AC | 19 ms | 3064 KB |
1_013.txt | AC | 22 ms | 3188 KB |
1_014.txt | AC | 38 ms | 3188 KB |
1_015.txt | AC | 21 ms | 3316 KB |
1_016.txt | AC | 21 ms | 3316 KB |
1_017.txt | AC | 25 ms | 3316 KB |
1_018.txt | AC | 373 ms | 3316 KB |
1_019.txt | AC | 302 ms | 40608 KB |
1_020.txt | AC | 307 ms | 38500 KB |
1_021.txt | AC | 314 ms | 38372 KB |
1_022.txt | AC | 699 ms | 37348 KB |