forked from abeaumont/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd.py
More file actions
executable file
·31 lines (29 loc) · 668 Bytes
/
Copy pathd.py
File metadata and controls
executable file
·31 lines (29 loc) · 668 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env python3
# https://arc097.contest.atcoder.jp/tasks/arc097_b
import resource, sys
resource.setrlimit(resource.RLIMIT_STACK, [0x10000000, resource.RLIM_INFINITY])
sys.setrecursionlimit(100000)
n, m = map(int, input().split())
p = [int(x) - 1 for x in input().split()]
g = [list() for _ in range(n)]
for _ in range(m):
u, v = map(int, input().split())
u -= 1
v -= 1
g[u].append(v)
g[v].append(u)
v = [False] * n
s = set()
def dfs(i):
if v[i]: return
s.add(i)
v[i] = True
for k in g[i]: dfs(k)
c = 0
for i in range(n):
if v[i]: continue
s = set()
dfs(i)
for k in s:
if p[k] in s: c += 1
print(c)