We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 806d85a commit 186f75bCopy full SHA for 186f75b
YoonYn9915/Graph/2024-09-30-[백준]-#2644-촌수계산.py
@@ -0,0 +1,30 @@
1
+
2
+N = int(input())
3
+A, B = map(int, input().split())
4
+M = int(input())
5
+graph = [[] for _ in range(N+1)]
6
+visited = [False] * (N+1)
7
+result = []
8
9
+for _ in range(M):
10
+ x, y = map(int, input().split())
11
+ graph[x].append(y)
12
+ graph[y].append(x)
13
14
+# dfs
15
+def dfs(v, num):
16
+ num += 1
17
+ visited[v] = True
18
19
+ if v == B:
20
+ result.append(num)
21
22
+ for i in graph[v]:
23
+ if not visited[i]:
24
+ dfs(i, num)
25
26
+dfs(A, 0)
27
+if len(result) == 0:
28
+ print(-1)
29
+else:
30
+ print(result[0]-1)
0 commit comments