We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent d13edc6 commit 6f7469dCopy full SHA for 6f7469d
minjeong/DFSBFS/2025-08-04-[백준]-#2644-촌수계산.py
@@ -0,0 +1,31 @@
1
+import sys
2
+input = sys.stdin.readline
3
+
4
+def dfs(start, cnt):
5
+ global chonsu
6
+ if start == y:
7
+ chonsu = cnt
8
+ return
9
10
+ for i in graph[start]:
11
+ if not visited[i]:
12
+ visited[i] = True
13
+ dfs(i, cnt + 1)
14
+ visited[i] = False
15
16
17
+n = int(input()) # 전체 사람 개수
18
+x, y = map(int, input().split())
19
+graph = [[] for _ in range(n+1)]
20
+visited = [False for _ in range(n + 1)]
21
+chonsu = -1 # 정답으로 반환할 촌수
22
23
+m = int(input()) # 부모 자식들간의 관계 개수
24
+for _ in range(m):
25
+ a, b = map(int, input().split())
26
+ graph[a].append(b)
27
+ graph[b].append(a)
28
29
+visited[x] = True
30
+dfs(x, 0)
31
+print(chonsu)
0 commit comments