본문 바로가기
카테고리 없음

[백준] 13023 - ABCDE Python

by 들숨날숨흡 2023. 5. 25.
728x90

https://www.acmicpc.net/problem/13023

 

13023번: ABCDE

문제의 조건에 맞는 A, B, C, D, E가 존재하면 1을 없으면 0을 출력한다.

www.acmicpc.net

 

import sys
from collections import defaultdict

def dfs(position, depth):
    global finished
    visited[position] = 1
    if depth == 4:
        finished = True
        return
    for i in relationship[position]:
        if not visited[i]:
            dfs(i, depth + 1)
            visited[i] = 0

N, M = map(int, input().split(" "))
relationship = defaultdict(list)
visited = [0] * N
finished = False

for i in range(M):
    a, b = map(int, input().split(" "))
    relationship[a].append(b)
    relationship[b].append(a)

for i in range(N):
    dfs(i, 0)
    visited[i] = 0
    if finished:
        break

if finished:
    print(1)
else:
    print(0)
728x90