Skip to content

Commit 186f75b

Browse files
committed
[BOJ] 촌수계산 / 실버2 / 40분
https://www.acmicpc.net/problem/2644
1 parent 806d85a commit 186f75b

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)