문제
나의 풀이
1. 서로소 집합 알고리즘
def find_parent(parent,x):
if parent[x] != x:
parent[x] = find_parent(parent, parent[x])
return parent[x]
def union_parent(parent, a, b):
a = find_parent(parent, a)
b = find_parent(parent, b)
if a < b:
parent[b] = a
else:
parent[a] = b
n, m = map(int, input().split())
parent = list(range(n + 1))
for i in range(1, n + 1):
arr = list(map(int, input().split()))
for j in range(n):
if arr[j]:
union_parent(parent, i, j + 1)
go = list(map(int, input().split()))
cycle = True
for i in range(m - 1):
if find_parent(parent, go[i]) != find_parent(parent, go[i + 1]):
cycle = False
print('YES') if cycle else print('NO')