문제풀이/백준(Boj) 문제풀이 188

[백준][그래프] - 2606. 바이러스

[다시 푸는 문제] [DFS] def DFS(start): global answer visited[start] = 1 for i in network[start]: if visited[i] == 0: DFS(i) answer += 1 if __name__ == "__main__": computer = int(input()) pair_Computer = int(input()) network = [[] for _ in range(computer+1)] visited = [0] * (computer + 1) answer = 0 for i in range(pair_Computer): a,b = map(int,input().split()) network[a].append(b) network[b].append(a) ..

[백준][그래프] - 1260. DFS와 BFS

[다시 풀어보는 문제] import collections def DFS(graph, v, visited): visited[v] = True print(v, end=" ") for i in graph[v]: if not visited[i]: DFS(graph,i,visited) def BFS(graph, v, visited): q = collections.deque([v]) visited = [False] * (n+1) visited[v] = True while q: now = q.popleft() print(now, end=" ") for i in graph[now]: if not visited[i]: q.append(i) visited[i] = True if __name__=="__main__": n,..

[백준][자료구조/힙] 2638. 치즈(파이썬/Python)

from collections import deque n,m=map(int,input().split()) graph=[list(map(int, input().split())) for i in range(n)] c= 0 dx=[-1, 0, 1, 0] dy=[0, 1, 0, -1] def bfs(): q=deque() q.append([0,0]) visited=[[-1]*m for i in range(n)] visited[0][0] = 0 while q: x, y = q.popleft() for i in range(4): nx = x + dx[i] ny = y + dy[i] if 0 = 3: graph[i][j] = 0 flag = 1 elif graph[i][j] == 2: graph[i][j] = 1 i..

[백준][BFS] 1389. 케빈 베이컨의 6단계 법칙 (파이썬/Python)

import sys input = sys.stdin.readline from collections import deque def bfs(graph,start): q = deque() q.append(start) visited[start] = 1 distance = [0] * (n+1) while q: x = q.popleft() for i in graph[x]: if visited[i] == 0: distance[i] = distance[x] + 1 q.append(i) visited[i] = 1 return sum(distance) n,m = map(int,input().split()) answer = [] graph = [[] for _ in range(n+1)] for _ in range(m): a..

[백준][자료구조/힙] 1202. 보석도둑(파이썬/Python)

import sys import heapq input = sys.stdin.readline n,k = map(int,input().split()) bag = [] jewely = [] candidate = [] answer = 0 for _ in range(n): weight, price = map(int,input().split()) heapq.heappush(jewely,[weight,price]) for _ in range(k): bag.append(int(input())) bag.sort() for i in bag: while jewely and i >= jewely[0][0]: weight, price = heapq.heappop(jewely) heapq.heappush(candidate,-pr..