Notice
Recent Posts
Recent Comments
Link
HwangHub
[DFS] 백준 13023. ABCDE 본문
13023번: ABCDE
문제의 조건에 맞는 A, B, C, D, E가 존재하면 1을 없으면 0을 출력한다.
www.acmicpc.net
문제 해석 : 노드의 관계를 묻고 있으니 그래프 문제이고, 모든 노드가 연결되어 있으면 되므로 깊이가 5가 되면 된다. (1부터 시작)
"""
사람 수 N : 5 <= N <= 2,000
친구 관계 수 M : 1 <= M <= 2,000
"""
def dfs(graph, node, visited, depth):
global ans
if depth == 5: ans = 1; return
for i in graph[node]:
if not visited[i]:
visited[node] = True
dfs(graph, i, visited, depth+1)
visited[i] = False
N, M = map(int, input().split())
graph = [[] for _ in range(N)]
visited = [False] * N
ans = 0
for _ in range(M):
a, b = map(int, input().split())
graph[a].append(b)
graph[b].append(a)
for i in range(N):
visited[i] = True
dfs(graph, i,visited, 1)
visited[i] = False
print(ans)
'workspace > 알고리즘' 카테고리의 다른 글
[다익스트라] 백준 1753. 최단경로 (0) | 2023.05.20 |
---|---|
[DFS/BFS] 백준 11724. 연결 요소의 개수 (0) | 2023.05.17 |
[BFS] 2644 촌수 계산 (1) | 2023.05.12 |
[data structure/tree] 백준 1991. 트리 순회 (0) | 2023.05.12 |
[DP] 11726. 2 x N 타일링 (0) | 2023.05.09 |
Comments