-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1976.py
More file actions
30 lines (26 loc) · 705 Bytes
/
1976.py
File metadata and controls
30 lines (26 loc) · 705 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
def is_connected(a, b, visited):
if board[a][b] == 1:
return True
for i in range(n):
if board[a][i] == 1 and i not in visited:
visited.append(i)
if is_connected(i, b, visited):
return True
return False
n = int(input())
m = int(input())
board = []
for i in range(n):
board.append(list(map(int, input().split())))
for i in range(n):
board[i][i] = 1
travel = list(map(int, input().split()))
before_city = travel[0] - 1
for i in range(1, len(travel)):
cur_city = travel[i] - 1
if is_connected(before_city, cur_city, [before_city]):
before_city = cur_city
else:
print("NO")
exit()
print("YES")