13023: ABCDE

ewillwin·2023년 5월 6일
0

Problem Solving (BOJ)

목록 보기
45/230

  • adjacent list를 이용하여 graph를 구현함
  • 해당 graph를 dfs를 통해 순회하면서, depth가 5인 경우가 생기면 possbile_flag = 1로 할당
  • 조건에 맞는 A, B, C, D, E가 존재하면 1 출력 -> dfs의 depth가 5면 1 출력
  • depth 1부터 시작/ booltype 원소가 들어있는 visit list를 사용
  • main에서 node들 (사람들)을 for문을 돌며 하나씩 root node로 설정하여 dfs 순회함
import sys

tmp = list(map(int, sys.stdin.readline()[:-1].split(' ')))
N = tmp[0]; M = tmp[1]
adjacent = [[] for _ in range(N)]
for _ in range(M):
    t0, t1 = map(int, sys.stdin.readline()[:-1].split(' '))
    adjacent[t0].append(t1)
    adjacent[t1].append(t0)

visit = [False] * N
possible_flag = 0
def dfs(start, depth):
    global possible_flag
    visit[start] = True
    if depth == 5:
        possible_flag = 1
        return
    for i in range(len(adjacent[start])):
        if not visit[adjacent[start][i]]:
            dfs(adjacent[start][i], depth+1)
    visit[start] = False

for x in range(N):
    dfs(x, 1)

if possible_flag == 1:
    print(1)
else:
    print(0)
profile
Software Engineer @ LG Electronics

0개의 댓글