-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[트리] 5월 23일 #17
base: main
Are you sure you want to change the base?
[트리] 5월 23일 #17
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
필수 1/2문제, 선택 3/5문제 확인되셨습니다.
남은 필수문제 꼭 추가제출로 채워주세요!
과제하시느라 고생하셨습니다😊
|
||
remove_student = photo[0] # 삭제할 학생 번호 | ||
min_vote = students[photo[0]][0] # 최소 추천 수 | ||
for p in photo[1:]: | ||
if students[p][0] < min_vote: # 추천 수가 더 작으면 | ||
min_vote = students[p][0] | ||
remove_student = p |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
사실 이 문제의 경우 n이 크지 않아서 그냥 정렬을 하셔도 돼요...!
#삭제할 노드를 시작으로 트리 순회, 삭제했음을 표시 | ||
q = deque() | ||
q.append(remove_node) | ||
while q: | ||
root = q.popleft() | ||
#다음 방문할 노드 추가 | ||
for child in tree[root]: | ||
q.append(child) | ||
tree[root] = [-1] #삭제된 노드임을 표시 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
p2. 지금 방법도 좋지만, 위에서 이미 삭제할 노드는 자식 노드로서 추가를 하지 않으셨기 때문에 큐를 이용한 탐색으로 루트부터 진행하며 리프노드를 셀 수도 있을거 같아요!
def graph_to_tree(root): | ||
q = deque() | ||
visited = [False]*(n+1) | ||
visited[root] = True | ||
q.append(root) | ||
|
||
while q: # 큐가 빌때까지 | ||
parent = q.popleft() | ||
for child in graph[parent]: | ||
if visited[child]: # 이미 방문했으면 넘어가기 | ||
continue | ||
visited[child] = True | ||
tree[parent].append(child) | ||
q.append(child) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
p2. 현재 풀이도 좋아요! 그런데, 그래프를 꼭 트리로 바꿔야할지 한번 고민해보시면 좋을거 같아요.
바꾸지 않고 기존 코드 그대로 사용했을 때 어떤 문제점이 생길지, 그 문제를 더 간단하게 해결할 수 있는지요!
for key in lines[1:]: | ||
parent = root | ||
key = int(key) | ||
tree[key] = [None, None] | ||
|
||
#key가 들어갈 위치 찾기 | ||
while True: | ||
#root보다 key값이 작으면 | ||
if key < parent: | ||
#삽입 가능 | ||
if not tree[parent][0]: | ||
tree[parent][0] = key | ||
break | ||
#이미 다른 값이 있으면 | ||
else: | ||
parent = tree[parent][0] | ||
else: | ||
#삽입 가능 | ||
if not tree[parent][1]: | ||
tree[parent][1] = key | ||
break | ||
#이미 다른 값이 있으면 | ||
else: | ||
parent = tree[parent][1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
풀이 너무 좋아요👍
올라온 샘플코드 중 v2도 한번 읽어주시면 좋을거 같아요!
양궁대회 추가제출 했습니다!! |
내용 & 질문
<기존 제출>
<추가 제출>