Skip to content

Commit

Permalink
[boj] 1260 solved
Browse files Browse the repository at this point in the history
  • Loading branch information
haremeat committed Dec 17, 2021
1 parent 631ca5d commit 85863ec
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 65 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* [단어의 개수](https://github.com/haremeat/Algorithm/blob/boj/main/1152.py)
* [단어 공부](https://github.com/haremeat/Algorithm/blob/main/boj/1157.py)
* [숫자의 합](https://github.com/haremeat/Algorithm/blob/main/boj/11720.py)
* [DFS와 BFS](https://github.com/haremeat/Algorithm/blob/main/boj/1260.py)
* [날짜 계산](https://github.com/haremeat/Algorithm/blob/main/boj/1476.py)
* [손익분기점](https://github.com/haremeat/Algorithm/blob/main/boj/1712.py)
* [수 이어 쓰기 1](https://github.com/haremeat/Algorithm/blob/main/boj/1748.py) - `미해결`
Expand Down Expand Up @@ -51,5 +50,5 @@
* [2×n 타일링 2](https://github.com/haremeat/Algorithm/blob/main/boj/11727.py)
* [1, 2, 3 더하기](https://github.com/haremeat/Algorithm/blob/main/boj/9095.py)
* [카드 구매하기](https://github.com/haremeat/Algorithm/blob/main/boj/11052.py)
* [가장 긴 증가하는 부분 수열](https://github.com/haremeat/Algorithm/blob/main/boj/11053.py) - `미해결`

* [가장 긴 증가하는 부분 수열](https://github.com/haremeat/Algorithm/blob/main/boj/11053.py)
* [DFS와 BFS](https://github.com/haremeat/Algorithm/blob/main/boj/1260.py)
124 changes: 62 additions & 62 deletions boj/1260.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,86 +8,86 @@
# test1


from collections import deque

n, m, v = map(int, input().split())

graph = [[0] * (n + 1) for _ in range(n + 1)]

for i in range(m):
m1, m2 = int(input().split())
graph[m1][m2] = graph[m2][m1] = 1


def bfs(start, visited):
queue = deque([start])

visited[start] = True

while queue:
v = queue.popleft()
print(v, end=' ')

for i in graph[v]:
if not visited[i]:
queue.append(i)
visited[i] = True


def dfs(v, visited=[]):
# 현재 노드를 방문처리
visited[v] = True
print(v, end=' ')

# 현재 노드와 연결된 다른 노드를 재귀적으로 방문
for i in graph[v]:
if not visited[i]:
dfs(graph, i, visited)


dfs(v)
print()
bfs(v)

# from collections import deque
#
# n, m, v = map(int, input().split())
#
# graph = [[0] * (n + 1) for _ in range(n + 1)]
#
# for _ in range(m):
# m1, m2 = map(int, input().split())
# # 노드 연결하기
# for i in range(m):
# m1, m2 = int(input().split())
# graph[m1][m2] = graph[m2][m1] = 1
#
# # 너비 우선 탐색
# def bfs(start_v):
# discoverd = [start_v]
# queue = deque()
# queue.append(start_v)
#
# def bfs(start, visited):
# queue = deque([start])
#
# visited[start] = True
#
# while queue:
# v = queue.popleft()
# print(v, end=' ')
#
# for w in range(len(graph[start_v])):
# if graph[v][w] == 1 and (w not in discoverd):
# discoverd.append(w)
# queue.append(w)
# for i in graph[v]:
# if not visited[i]:
# queue.append(i)
# visited[i] = True
#
#
# def dfs(v, visited=[]):
# # 현재 노드를 방문처리
# visited[v] = True
# print(v, end=' ')
#
# # 깊이 우선 탐색
# def dfs(start_v, discoverd=[]):
# discoverd.append(start_v)
# print(start_v, end=' ')
#
# for w in range(len(graph[start_v])):
# if graph[start_v][w] == 1 and (w not in discoverd):
# dfs(w, discoverd)
#
# # 현재 노드와 연결된 다른 노드를 재귀적으로 방문
# for i in graph[v]:
# if not visited[i]:
# dfs(graph, i, visited)
#
#
# dfs(v)
# print()
# bfs(v)

from collections import deque

n, m, v = map(int, input().split())

graph = [[0] * (n + 1) for _ in range(n + 1)]

for _ in range(m):
m1, m2 = map(int, input().split())
# 노드 연결하기
graph[m1][m2] = graph[m2][m1] = 1

# 너비 우선 탐색
def bfs(start_v):
discoverd = [start_v]
queue = deque()
queue.append(start_v)

while queue:
v = queue.popleft()
print(v, end=' ')

for w in range(len(graph[start_v])):
if graph[v][w] == 1 and (w not in discoverd):
discoverd.append(w)
queue.append(w)



# 깊이 우선 탐색
def dfs(start_v, discoverd=[]):
discoverd.append(start_v)
print(start_v, end=' ')

for w in range(len(graph[start_v])):
if graph[start_v][w] == 1 and (w not in discoverd):
dfs(w, discoverd)



dfs(v)
print()
bfs(v)

0 comments on commit 85863ec

Please sign in to comment.