Skip to content

Commit

Permalink
2024-02-20 solved
Browse files Browse the repository at this point in the history
  • Loading branch information
alstjr7437 committed Feb 20, 2024
1 parent 30f3dc6 commit 34855da
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions alstjr7437/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@
| 7차시 | 2024.02.08 | 스택 | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/12909">올바른 괄호</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/22 |
| 8차시 | 2024.02.14 | DP | <a href="https://www.acmicpc.net/problem/12865">평범한 배낭</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/24 |
| 9차시 | 2024.02.17 | 그리디 | <a href="https://www.acmicpc.net/problem/2138">전구와 스위치</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/29 |
| 10차시 | 2024.02.20 | BFS/DFS | <a href="https://www.acmicpc.net/problem/11725">전구와 스위치</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/33 |

46 changes: 46 additions & 0 deletions alstjr7437/트리/트리의-부모-찾기.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import sys
sys.setrecursionlimit(10**8) #재귀 리미트
from collections import deque

n = int(input())

# 트리 만들기
tree = {i:[] for i in range(1,n+1)}
for i in range(n-1):
a, b = map(int, input().split())
tree[a].append(b)
tree[b].append(a)
# print(tree)

# 부모 저장하기
visited = [0] * (n+1)

# DFS
def dfs(a):
for i in tree[a]:
if visited[i] == 0:
visited[i] = a
# print(visited, i, a)
dfs(i)

# BFS
def bfs():
while queue:
a = queue.popleft()
for i in tree[a]:
if visited[i] == 0:
queue.append(i)
visited[i] = a
# print(visited, queue)


visited[1] = '어무이'

# dfs(1)

queue = deque()
queue.append(1)
bfs()

for i in range(2, n+1):
print(visited[i])

0 comments on commit 34855da

Please sign in to comment.