forked from satnaing/astro-paper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Affected files: .obsidian/workspace.json src/content/blog/boj-2660-회장뽑기.md
- Loading branch information
Showing
2 changed files
with
91 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 함수를 적극 이용해 봤습니다. (코드를 줄이기 위해서...?!) |