๋ฌธ์
https://www.acmicpc.net/problem/13023
ํ์ด
# ๋ฐฑ์ค 13023๋ฒ ๋ฌธ์ - ABCDE
# dfs
import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000)
n, m = map(int , input().split())
visited = [0]*(n)
graph = [ [] for _ in range(n) ]
answer = 0
for _ in range(m):
a,b = map(int, input().split())
graph[a].append(b)
graph[b].append(a)
def dfs(start , depth):
global answer
visited[start] = 1
if depth == 4:
answer = True
return
for i in graph[start]:
if visited[i] == 0:
dfs(i , depth+1)
visited[start]=0
for i in range(n):
dfs(i ,0)
if answer:
break
if answer:
print(1)
else:
print(0)
DFS๋ก ํ ์ ์๋ ๋ฌธ์ ์ด๋ค.
์น๊ตฌ๋ค์ ๊ด๊ณ๋ฅผ graph์ ๋ฃ์ด์ฃผ๊ณ ๋ฐฉ๋ฌธ์ฒ๋ฆฌ๋ฅผ ํตํด ์ฌ๊ท๋ฅผ ๋๋ฉด ๋๋ค.
'๐๏ธ Algorithm > ๐ฉ ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ฉ [๋ฐฑ์ค] [Python] [Gold2] 12015๋ฒ_๊ฐ์ฅ ๊ธด ์ฆ๊ฐํ๋ ๋ถ๋ถ ์์ด 2 (0) | 2023.05.12 |
---|---|
๐ฉ [๋ฐฑ์ค] [Python] [Gold4] 1043๋ฒ_๊ฑฐ์ง๋ง (0) | 2023.05.12 |
๐ฉ [๋ฐฑ์ค] [Python] [Gold3] 1238๋ฒ_ํํฐ (1) | 2023.05.09 |
๐ฉ [๋ฐฑ์ค] [Python] [Gold4] 11054๋ฒ_๊ฐ์ฅ ๊ธด ๋ฐ์ดํ ๋ ๋ถ๋ถ ์์ด (1) | 2023.05.09 |
๐ฉ [๋ฐฑ์ค] [Python] [Gold4] 14500๋ฒ_ํ ํธ๋ก๋ฏธ๋ ธ (1) | 2023.05.08 |