Skip to content

Commit 9fc08af

Browse files
authored
Merge pull request #41 from YoonYn9915/main
YoonYn9915 / 7월 2주차 2문제
2 parents 9593bd2 + 3415411 commit 9fc08af

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from collections import defaultdict
2+
from collections import deque
3+
4+
5+
def bfs(graph, visited):
6+
queue = deque()
7+
8+
queue.append(1)
9+
visited[1] = 1
10+
answer = 0
11+
12+
while queue:
13+
num = queue.popleft()
14+
for neighbor in graph[num]:
15+
if visited[neighbor] == 0:
16+
queue.append(neighbor)
17+
visited[neighbor] = 1
18+
answer += 1
19+
20+
print(answer)
21+
22+
23+
numOfComputer = int(input())
24+
numOfConnection = int(input())
25+
26+
graph = defaultdict(list)
27+
visited = [0] * (numOfComputer + 1)
28+
29+
for _ in range(numOfConnection):
30+
n1, n2 = map(int, input().split())
31+
graph[n1].append(n2)
32+
graph[n2].append(n1)
33+
34+
bfs(graph, visited)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
3+
n = int(input())
4+
5+
arr = [-1] * 1000001
6+
7+
arr[1] = 0
8+
arr[2] = 1
9+
arr[3] = 1
10+
11+
for i in range(4, n + 1):
12+
13+
arr[i] = arr[i - 1] + 1
14+
15+
if i % 3 == 0:
16+
if arr[i//3] + 1 < arr[i]:
17+
arr[i] = arr[i//3] + 1
18+
if i % 2 == 0:
19+
if arr[i//2] + 1 < arr[i]:
20+
arr[i] = arr[i//2] + 1
21+
22+
23+
print(arr[n])

0 commit comments

Comments
 (0)