diff --git "a/Dolchae/DFS&BFS/2644 \354\264\214\354\210\230\352\263\204\354\202\260.py" "b/Dolchae/DFS&BFS/2644 \354\264\214\354\210\230\352\263\204\354\202\260.py" new file mode 100644 index 0000000..d515960 --- /dev/null +++ "b/Dolchae/DFS&BFS/2644 \354\264\214\354\210\230\352\263\204\354\202\260.py" @@ -0,0 +1,28 @@ +n = int(input()) +a,b = map(int, input().split()) +node_num = int(input()) +graph = [[] for _ in range(n+1)] +visited = [False] * (n+1) +result = [] + +for _ in range(node_num): + x, y = map(int, input().split()) + graph[x].append(y) + graph[y].append(x) + +def dfs(v, num): + num += 1 + visited[v] = True + + if v == b: + result.append(num) + + for i in graph[v]: + if not visited[i]: + dfs(i, num) + +dfs(a, 0) +if len(result) == 0: + print(-1) +else: + print(result[0]-1) \ No newline at end of file diff --git a/Dolchae/README.md b/Dolchae/README.md index d0bcf9f..bda8bb4 100644 --- a/Dolchae/README.md +++ b/Dolchae/README.md @@ -30,4 +30,5 @@ | 26차시| 2024.02.05 | 구현 | [18110 solved.ac](https://www.acmicpc.net/problem/18110) | [#93](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/93) | | 27차시| 2024.02.08 | 정렬 | [1302 베스트셀러](https://www.acmicpc.net/problem/1302) | [#97](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/97) | | 28차시| 2024.02.15 | 이진 탐색 | [6236 용돈 관리](https://www.acmicpc.net/problem/1302) | [#107](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/107) | -| 29차시| 2024.02.15 | 브루트포스 | [2961 도영이가 만든 맛있는 음식](https://www.acmicpc.net/problem/2961) | [#109](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/109) | \ No newline at end of file +| 29차시| 2024.02.19 | 브루트포스 | [2961 도영이가 만든 맛있는 음식](https://www.acmicpc.net/problem/2961) | [#109](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/109) | +| 30차시| 2024.02.22 | DFS | [2644 촌수계산](https://www.acmicpc.net/problem/2961) | [#112](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/112) | \ No newline at end of file