Skip to content

Commit 7cb4fcc

Browse files
committed
[BOJ] #25195. Yes or yes / 골드4 / 51분 / 성공
1 parent 9ca82e0 commit 7cb4fcc

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import sys
2+
sys.setrecursionlimit(10**6) # 재귀 한도
3+
4+
input = sys.stdin.readline
5+
6+
def dfs(current):
7+
if not visited[current]:
8+
if current not in gomgom: # 해당 노드에 팬클럽 곰곰이가 없다면, 방문 표시
9+
visited[current] = True
10+
else:
11+
# 시작부터 곰곰이 있기 때문에 True가 나올 수가 없기에 False 반환
12+
return False
13+
14+
if not graph[current]: # 더이상 탐색할 노드가 없다면, 팬클럽을 만나지 않아도 되기 때문에 True 반환
15+
return True
16+
17+
for node in graph[current]:
18+
if not visited[node]:
19+
visited[node] = True
20+
if node not in gomgom: # 인접 노드에 팬클럽이 없다면, 재귀호출
21+
if dfs(node): # 팬클럽을 만나지 않고 도착할 수 있는 경우 True 반환
22+
return True
23+
visited[node] = False # 다른 경로를 탐색하기 위해 방문취소
24+
25+
return False
26+
27+
# 정점의 개수 n, 간선의 개수 m
28+
n, m = map(int, input().split())
29+
graph = [[] for _ in range(n+1)]
30+
visited = [False] * (n+1)
31+
for _ in range(m):
32+
u, v = map(int, input().split())
33+
graph[u].append(v) # u에서 v로의 단방향 간선 추가
34+
35+
# 팬클럽 곰곰이 정보
36+
s = int(input()) # 곰곰이가가 존재하는 정점의 개수 s
37+
gomgom = list(map(int, input().split())) # 공곰이가 존재하는 정점의 번호 목록
38+
39+
# 1번 정점에서 dfs 탐색 시작
40+
if dfs(1): # 팬클럽 곰곰이를 만나지 않고 이동할 수 있는 경우
41+
print("yes")
42+
else: # 어떻게 이동하는 곰곰이를 만나게 되는 경우
43+
print("Yes")

0 commit comments

Comments
 (0)