Skip to content

Commit

Permalink
2024-07-07 20:33:05
Browse files Browse the repository at this point in the history
Affected files:
.obsidian/workspace.json
src/content/blog/boj-2660-회장뽑기.md
  • Loading branch information
gyunseo committed Jul 7, 2024
1 parent 1102abd commit c0ad62a
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 28 deletions.
38 changes: 10 additions & 28 deletions .obsidian/workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,16 @@
"type": "split",
"children": [
{
"id": "0fb89b2a6940e666",
"id": "ea8dbc547ef84373",
"type": "tabs",
"children": [
{
"id": "d150f0dbbe8f52e7",
"id": "443ea8f1c7a755e2",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "src/content/blog/leet-code-1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.md",
"mode": "source",
"source": false
}
}
}
]
},
{
"id": "932958f597546de7",
"type": "tabs",
"children": [
{
"id": "a847079fc4a2b861",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "src/content/blog/boj-1309-동물원.md",
"file": "src/content/blog/boj-2660-회장뽑기.md",
"mode": "source",
"source": false
}
Expand Down Expand Up @@ -103,7 +85,7 @@
"state": {
"type": "backlink",
"state": {
"file": "src/content/blog/boj-1309-동물원.md",
"file": "src/content/blog/boj-2660-회장뽑기.md",
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical",
Expand All @@ -120,7 +102,7 @@
"state": {
"type": "outgoing-link",
"state": {
"file": "src/content/blog/boj-1309-동물원.md",
"file": "src/content/blog/boj-2660-회장뽑기.md",
"linksCollapsed": false,
"unlinkedCollapsed": true
}
Expand All @@ -143,7 +125,7 @@
"state": {
"type": "outline",
"state": {
"file": "src/content/blog/boj-1309-동물원.md"
"file": "src/content/blog/boj-2660-회장뽑기.md"
}
}
}
Expand All @@ -166,10 +148,11 @@
"table-editor-obsidian:Advanced Tables Toolbar": false
}
},
"active": "a847079fc4a2b861",
"active": "443ea8f1c7a755e2",
"lastOpenFiles": [
"src/content/blog/leet-code-1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.md",
"src/content/blog/boj-1309-동물원.md",
"src/content/blog/boj-2660-회장뽑기.md",
"src/content/blog/leet-code-1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.md",
"src/content/blog/leet-code-438-find-all-anagrams-in-a-string.md",
"src/content/blog/boj-10159-저울.md",
"src/content/blog/leet-code-64-minimum-path-sum.md",
Expand Down Expand Up @@ -212,7 +195,6 @@
"src/content/blog/boj-15666-N과-M-(12).md",
"src/content/blog/boj-15664-N과-M-(10).md",
"src/content/blog/boj-15663-N과-M-(9).md",
"src/content/blog/boj-11728-배열-합치기.md",
"src/content/blog/boj-10819-차이를-최대로.md"
"src/content/blog/boj-11728-배열-합치기.md"
]
}
81 changes: 81 additions & 0 deletions src/content/blog/boj-2660-회장뽑기.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
author: Gyunseo Lee
title: "BOJ 백준 2660: 회장뽑기"
pubDatetime: 2024-07-07T19:00:00+09:00
modDatetime: 2024-07-07T19:00:00+09:00
featured: false
draft: false
tags:
- PS
- BOJ
- Algorithms
- BFS
- 실랜디
- 골랜디
description: 항상 문제를 읽고 종이와 펜으로 먼저 풀어 보자...
ogImage: ""
---

## Table of contents

## 들어가며

걸린 시간: 24분

사실 처음에 문제를 읽었을 때 무슨 말인지 잘 이해가 안 갔습니다.
그럴 때에는 억지로 문제를 이해하려 들지 말고, 주어진 input값을 직접 simulation하면 잘 이해가 되는 거 같더라고요.

## 접근

![](https://res.cloudinary.com/gyunseo-blog/image/upload/f_auto/v1720351765/image_gyfx0g.png)

결국 문제의 핵심은 그래프 탐색을 몇 번만에 다 할 수 있냐를 각 노드마다 시작점으로 줘서 구하는 것이었어요.
다행히 N은 최대 50이어서, 어렵지 않게 모든 노드에 대해 각각 1번씩 BFS를 해주어서 쉽게 답을 구해낼 수 있었답니다.

## 구현

```python
import sys
input = sys.stdin.readline
from collections import deque

def BFS(start):
distance[start] = 1
q = deque()
q.append(start)

while q:
cur_node = q.popleft()
for next_node in graph[cur_node]:
if distance[next_node] > 0:
continue
distance[next_node] = distance[cur_node] + 1
q.append(next_node)


if __name__ == "__main__":
N = int(input().strip())
graph = [[] for _ in range(N + 1)]
distance = [0 for _ in range(N + 1)]
ansList = []
minStepsToTraverseAllNodes = int(1e9)
while True:
u, v = map(int, input().strip().split())
if u == -1 and v == -1:
break
graph[u].append(v)
graph[v].append(u)
for start_node in range(1, N + 1):
BFS(start_node)
stepsToTraverseAllNodes = max(distance) - 1
minStepsToTraverseAllNodes = min(minStepsToTraverseAllNodes, stepsToTraverseAllNodes)
ansList.append((start_node, stepsToTraverseAllNodes))
distance = [0 for _ in range(N + 1)]

ansList = [*map(lambda x: x[0], filter(lambda x: x[1] == minStepsToTraverseAllNodes, ansList))]
print(minStepsToTraverseAllNodes, len(ansList))
print(" ".join(map(str, ansList)))
# N이 0, 1일 때는 처리를 안 해줘도 되나..?
```

filter, map 함수를 적극 이용해 봤습니다. (코드를 줄이기 위해서...?!)

0 comments on commit c0ad62a

Please sign in to comment.