| ps | |
|---|---|
| 링크 | acmicpc.net/… |
| 출처 | BOJ |
| 문제 번호 | 1976 |
| 문제명 | 여행 가자 |
| 레벨 | 골드 4 |
| 분류 |
Disjoint Set |
| 시간복잡도 | O(m + n^2*α(n)) |
| 인풋사이즈 | m<=1000, n<=200 |
| 사용한 언어 | Python |
| 제출기록 | 29200KB / 88ms |
| 최고기록 | 60ms |
| 해결날짜 | 2021/10/14 |
| 태그 | |
"""Solution code for "BOJ 1976. 여행 가자".
- Problem link: https://www.acmicpc.net/problem/1976
- Solution link: http://www.teferi.net/ps/problems/boj/1976
Tags: [DisjointSet]
"""
from teflib import disjointset
def main():
N = int(input())
M = int(input()) # pylint: disable=unused-variable
dsu = disjointset.DisjointSet(N)
for i in range(N):
cities = input().split()
for j, is_connected in enumerate(cities):
if is_connected == '1':
dsu.union(i, j)
targets = [int(x) for x in input().split()]
first = dsu.find(targets[0] - 1)
is_all_reachable = all(dsu.find(target - 1) == first for target in targets)
print('YES' if is_all_reachable else 'NO')
if __name__ == '__main__':
main()