Skip to content

Latest commit

 

History

History
72 lines (58 loc) · 1.45 KB

swea_graph_dir.md

File metadata and controls

72 lines (58 loc) · 1.45 KB

swea graph dir 4871

image

  • input
3
6 5
1 4
1 3
2 3
2 5
4 6
1 6
7 4
1 6
2 3
2 6
3 5
2 5
9 9
2 6
4 7
5 7
1 5
2 9
3 9
4 8
5 3
7 8
1 9
  • solve
import sys
sys.stdin = open('../sample_input.txt', 'r')

def depth_first_search(ver):
    # 함수 호출되면 거기 방문한 것
    visited[ver] = 1
    # 작은 숫자부터 세기
    for next in range(1, V + 1):
        # 인접하고 미방문이면
        if adjM[ver][next] == 1 and visited[next] == 0:
            # 다음 함수 호출
            depth_first_search(next)

T = int(input())
for test_case in range(1, T + 1):
    V, E = map(int, input().split())

    visited = [0 for _ in range(V + 1)]

    adjM = [[0] * (V + 1) for _ in range(V + 1)]

    for i in range(E):
        s, g = map(int, input().split())
        adjM[s][g] = 1

    start, end = map(int, input().split())

    depth_first_search(start)

    ans = 0
    if visited[end] == 1:
        ans = 1

    print(f'#{test_case} {ans}')