단방향 그래프 dfs 탐색
def find_path(s,g):
global answer
if s==g:
answer = 1
return
for next_node in graph[s]:
if not visited[next_node]:
visited[next_node] = True
find_path(next_node,g)
for test in range(1,int(input())+1):
V, E = map(int,input().split())
graph = {i:[] for i in range(1,V+1)}
answer = 0
visited = {i:False for i in range(1,V+1)}
for _ in range(E):
a, b = map(int,input().split())
graph[a] += [b]
s,g = map(int,input().split())
visited[s] = True
find_path(s,g)
print(f'#{test} {answer}')
'practivceAlgorithm > swexpertacademy' 카테고리의 다른 글
[SWEA][Python] 4879 종이붙이기 (0) | 2021.08.15 |
---|---|
[SWEA][Python] 4873 반복문자 지우기 (0) | 2021.08.15 |
[SWEA][Python] 4866 괄호검사 (0) | 2021.08.15 |
[SWEA][Python] 4864 문자열 비교 (0) | 2021.08.15 |
[SWEA][Python] 4861 회문 (0) | 2021.08.15 |