From c0ad62a66bbb8fa9f1e17a9866d0c0d44c926081 Mon Sep 17 00:00:00 2001 From: Gyunseo Lee Date: Sun, 7 Jul 2024 20:33:06 +0900 Subject: [PATCH] 2024-07-07 20:33:05 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Affected files: .obsidian/workspace.json src/content/blog/boj-2660-회장뽑기.md --- .obsidian/workspace.json | 38 +++------ ...14\354\236\245\353\275\221\352\270\260.md" | 81 +++++++++++++++++++ 2 files changed, 91 insertions(+), 28 deletions(-) create mode 100644 "src/content/blog/boj-2660-\355\232\214\354\236\245\353\275\221\352\270\260.md" diff --git a/.obsidian/workspace.json b/.obsidian/workspace.json index 9eadb44e1..1fa11abe0 100644 --- a/.obsidian/workspace.json +++ b/.obsidian/workspace.json @@ -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 } @@ -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", @@ -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 } @@ -143,7 +125,7 @@ "state": { "type": "outline", "state": { - "file": "src/content/blog/boj-1309-동물원.md" + "file": "src/content/blog/boj-2660-회장뽑기.md" } } } @@ -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", @@ -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" ] } \ No newline at end of file diff --git "a/src/content/blog/boj-2660-\355\232\214\354\236\245\353\275\221\352\270\260.md" "b/src/content/blog/boj-2660-\355\232\214\354\236\245\353\275\221\352\270\260.md" new file mode 100644 index 000000000..e3bba715c --- /dev/null +++ "b/src/content/blog/boj-2660-\355\232\214\354\236\245\353\275\221\352\270\260.md" @@ -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 함수를 적극 이용해 봤습니다. (코드를 줄이기 위해서...?!)